> On Dec 3, 2015, at 2:04 AM, fugee ohu <fugee279@gmail.com> wrote:
>
> Why does <%= form_for(@listing) do |f| %> the form helpers f.select, f.label, f.text_field, f.checkbox, f.radio_button all prepend model names to fields like listings_title, listings_description or in my browsers source listings[title] listings[description] and to avoid this i have to write my own html instead of using the helpers
Are you trying to create a form field that is not bound to a model instance? The whole point of the "bound" helpers is to keep you from having to type the same thing over and over in the context of a form that modifies a particular instance of your model. The names of the fields it generates are designed to pass the correct nested hash of attribute values from the form to the controller, so you can do everything with very little code there (convention over configuration).
Rails also includes the "_tag" variants of these helpers, and they don't assume anything. You pass them the name, initial value, and attributes and you get a well-formed tag in the output. They don't use the form_for block attribute, either.
form_for @listing do |f|
f.text_field :name, class: 'name-field'
end
form_tag listing_path(@listing) do
text_field_tag 'listing[name]', @listing.name, class: 'name-field'
end
Unless I am mistaken, those are roughly equivalent, but the former has less typing required, fewer chances to get it wrong. If you just want a particular tag to be output in the context of a form_for (bound) form helper, you can mix and match the _tag variant inside there, just as you can also add hand-written tags (or indeed, any other HTML you like) inside the form_for block. You may want to do this for JavaScript reasons unrelated to the rest of your form, and excluded or ignored by your controller's update and create methods. But all this is a guess on my part, because you haven't outlined your actual issue here.
Walter
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/DF03CC5D-9186-4888-8F76-85ED3F5CF8FB%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment