Comments (32)
Now I see your problem. This is done on purpose because when you have a nested attribute, simple form will look at simple_form.labels.user.user_profile.firstname, allowing you to have custom names for nested attributes.
If you want however to use the same firstname across all labels, one alternative is to change the human_attribute_name instead (using I18n API for Rails itself)
Please try to include the maximum information possible when reporting a bug or issue. Since this really helps us to understand what is happening.
from simple_form.
Let's look at the source code:
http://github.com/plataformatec/simple_form/blob/master/lib/simple_form/inputs/base.rb#L119
SimpleForm uses the object_name. You can retrieve it by doing <%= profile_form.object_name %> inside your fields_for and I believe it will return: "user_profile" instead of "user.profile". Not exactly what I said, but the result at the end is the same. Could you please confirm?
Notice I18n allows you to shortcut to translations, so you can eventually do this:
en:
simple_form:
labels:
user_profile: :"simple_form.labels.profile"
This will link user_profile to the translations for labels profile.
from simple_form.
Can you please give more information? How is your model? How is your simple_form_for call? Can you produce a failing test case?
from simple_form.
The case is pretty simple, I think. There is a User model (devise model) which has_one UserProfile, and UserProfile belongs_to User. User "accepts_nested_attributes_for" UserProfile, and User has attr_accessible :profile.
The form looks like this:
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.simple_fields_for :profile do |profile_form| %>
<%= profile_form.firstname %>
<% end %>
<% end %>
from simple_form.
Err, except that your example would raise a NoMethodError? :) Do you mean profile_form.input :first_name?
Also, which keys are you expecting it to bring from I18n? Which ones is it bringing instead? The more information you give about an error the easier for it to get fixed.
from simple_form.
Ok. My mistake. The form says <%= profile_form.input :firstname %>
And here is the excerpt from the locale file:
en:
simple_form:
labels:
user_profile:
firstname: "名"
user:
email: "メールアドレス"
password: "パスワード"
In fact it does not bring any translation from locale yml.
from simple_form.
Thanks and can you clarify something here? You should use a "model name" or "association name" as a key in locale when writing translations.
For example, if the association is like:
class User < ActiveRecord::Base
has_one :profile, :class_name => 'UserProfile'
end
then locale yml should look like this?
en:
simple_form:
labels:
user:
profile:
firstname: "Abra kadabra"
?
from simple_form.
<%= profile_form.object_name %>
returned
user[profile_attributes]
from simple_form.
Ugh. It seems like a rails bug. Which Rails version are you using? object_name should return the underscore version. :(
from simple_form.
Please use the human_attribute_name translation while I investigate this issue. :) Thanks!
from simple_form.
I am using Rails 3 on ruby-1.9.2
from simple_form.
I also have this problem with Rails 3 on Ruby 1.9.2 with simple_form 1.2.2. You can use this format and it works OK but the underscore version would be prettier :-) :
"user[profile]":
firstname: "The Name"
from simple_form.
Vesan : You can also redirect everithing nested to the original translation
# config/locale/models/address/fr.yml fr: simple_form: hints: address: address_1: "Spécifiez votre numéro d'appartement au besoin" address_2: "Ce champ est facultatif" postal_code: "i.e. H0H0H0"
# config/locale/models/lead/fr.yml fr: simple_form: hints: lead[address_attributes]: :"simple_form.hints.address"
from simple_form.
I also have a similar problem with a "has_many" association, nevertheless I cannot use this solution because the object_name returned by Rails for this kind of association includes an index (so, redirecting for each index is not an alternative).
My test code is something like this:
class Contact < ActiveRecord::Base
has_many :emails
...
end
...
<%= f.simple_fields_for :emails do |fields| %>
<%= fields.object_name %>
<%= render 'emails/fields', :f => fields %>
<% end %>
with the result:
contact[emails_attributes][0]
Someone has any idea about how to work with this?
I think even if it's a Rails bug, including the orginal model attributes in the translation lookup queue in the SimpleForm::Inputs::Base#translate method could help...
from simple_form.
The reason object_name is the way it is, is because of the code here: https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/form_helper.rb#L1281
def fields_for_with_nested_attributes(association_name, args, block)
name = "#{object_name}[#{association_name}_attributes]"
The index get's appended a few lines below that:
output << fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, options, block)
from simple_form.
Yeah, I think SimpleForm will need to manipulate the object_name and get a readable version from it.
from simple_form.
# Extract the model names from the object_name mess.
def model_names
object_name.scan(/([a-zA-Z_]+)/).flatten.map{ |x| x.gsub('_attributes', '') }
end
The above will return an array of model names. For example:
route[blocks_attributes][0][blocks_learning_object_attributes][1][asdf_attributes]
will result in: ["route", "blocks", "blocks_learning_object", "asdf"]
Not immediately obvious to me how to integrate this in the SimpleForm::Inputs::Base.translate method.
from simple_form.
You can add a new lookup in the SimpleForm::Inputs::Base.translate method using this result.
from simple_form.
Sounds a good idea, guess I'll give it a show when I find some time here. This keeps bothering me once in a while. Thanks.
from simple_form.
so is the current status of this still to translate nested models, the format of
product[variations_attributes][0][shipping_profile_attributes]
is needed for when has_many is involved? im just setting generic translations, but its not the best solution.
from simple_form.
Improve i18n lookup for nested models, closed by 25abdac
from simple_form.
@jpzwarte I've used your scan method to improve the i18n lookup for nested models, it should be working fine now.
Could you guys please test with the master branch? Thanks.
from simple_form.
Thanks a lot!
from simple_form.
I've tried every sollution here, but still not working.
I'm using Rails 3.1 and a polymorphic association
class Produto < ActiveRecord::Base
has_one :item, :as => :tipo, :dependent => :destroy
end
class Item < ActiveRecord::Base
belongs_to :tipo, :polymorphic => true
end
simple_form:
labels:
produto:
item:
nome: "Anything"
from simple_form.
not working with rails 3.2 and ruby 1.9.3.
from simple_form.
@caarlos0 hi, what version of simple_form are you using? Could you push a simple app to github that reproduces this issue?
from simple_form.
version: 2.0.2
form is like this:
<%= simple_form_for(setup_birth(@birth), :html => {:class => 'form-horizontal'}) do |f| %>
<div class="well">
<legend> Dados do parto</legend>
<%= f.input :bith_date, :input_html => { :class => 'datepicker' }, :as => :string, :disabled => [email protected]_record? %>
<%= f.input :cow, :collection => Animal.inseminated_cows_collected, :disabled => [email protected]_record? %>
<%= f.input :obs, :input_html => { :cols => 50, :rows => 10, :style => "width: 400px" } %>
<% unless @birth.new_record? %>
<%= link_to "Editar filhote", edit_animal_path(@birth.child), :class => "btn" unless @birth.child.nil? %>
<% end %>
</div>
<% if @birth.new_record? %>
<div class="well">
<legend>Dados do filhote</legend>
<%= f.simple_fields_for :child_attributes do |ff| %>
<%= ff.input :earring %>
<%= ff.input :name %>
<%= ff.input :animal_type_id, :collection => AnimalType.all.collect { |t| [t.desc, t.id]} %>
<% end %>
</div>
<% end %>
<%= f.button :submit %>
<% end %>
pt.yml like this:
birth:
bith_date: 'Data Parto'
cow: 'Mãe'
obs: "Observação"
child:
earring: "Brinco"
name: "Nome"
animal_type_id: "Tipo de Animal"
I tried several things... nothing works.
from simple_form.
@caarlos0 hrm, I think here
<%= f.simple_fields_for :child_attributes do |ff| %>
you should use
<%= f.simple_fields_for :child do |ff| %>
no?
from simple_form.
ooops, my mistake.
Sorry.
Hey, maybe you can add something about this in docs, huh?
Thanks anyway.
cheers
from simple_form.
So does it work now? If so I don't think we should add something to SimpleForm's docs as fields_for
is a Rails's feature and it has a lot of documentation on it http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for
from simple_form.
hmm.. yeah, that's right.. sorry, I'm new on ruby/rails, when I research for nested forms I didn't find this documentations..
so, thank you again for the link :)
from simple_form.
no problem :)
from simple_form.
Related Issues (20)
- 5.2.0 applies `vertical_select` wrapper despite `wrapper: :horizontal_form` HOT 1
- Changelog entry needed for 5.2.0 HOT 3
- Is there any particular reason for the use of `%%` instead of `%` in the ERB template? HOT 2
- @builder.input_field(:license_plate, type: 'radio' .... ) output a textarea tag instead of an inpt HOT 1
- Broken Link in Readme File for Bootstrap Live App example HOT 4
- Chrome error: label's for attribute doesn't match any element id
- Bootstrap floating labels keep floating even without input HOT 1
- Auto reload configuration HOT 3
- `config.default_form_class` should not be *overriden* but *complemented* when `html: { :class }` is specified HOT 1
- Redundant aria-required on required fields HOT 1
- 5.3.0 - Custom Inputs Fail to Load HOT 3
- Rails 7.1 updated password validation changed rendering of password inputs
- Custom Component raise an error with rails 7.1 and zeitwerk HOT 1
- Hints don't work on the namespaced model HOT 1
- Encrypted attributes always default to textarea
- Proposal for customizable classes merging
- The custom `error:` on all inputs does not work if there is an object present
- Multiple inputs with the same name generate unparseable names in Rails >= 7.1.1
- <div class="checkbox"> <input value="false" type="hidden" name="meal[completed]"> <input class="boolean optional" type="checkbox" value="true" checked="checked" name="meal[completed]" id="meal_completed"> <label class="boolean optional" for="meal_completed">Completed</label> </div>Update: This bug only appears if you have custom value for `unchecked_value`. In this case `unchecked_value: false` HOT 1
- https://simple-form-bootstrap.herokuapp.com/ is Down HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simple_form.