<%= form_for(@listing) do |f| %> prepends model name to fields

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

actually it prepends in singular listing_title, listing_description, etc

i dunno, prints the result of the expression inside <% %> ?

On 3 Dec 2015 07:04, “fugee ohu” [email protected] 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

What do you think the = in <%= form_for does?

Colin


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected]
To post to this group, send email to [email protected]
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/aa7a1ebe-fea0-402f-8976-29874800608f%40googlegroups.com
.

So if i take out all the prependings of listing_ using my own html
instead
of the rails form helpers when i submit the form i’m gonna get errors?

On 3 December 2015 at 08:09, fugee ohu [email protected] wrote:

i dunno, prints the result of the expression inside <% %> ?

Sorry, I did not read your post carefully enough so asked the wrong
question.

Post (by copy/paste from your code) an example that shows the problem
and also the html that is generated by it.

Always a good idea to provide an example so that there is no confusion
about what is meant.

Colin

On Dec 3, 2015, at 2:04 AM, fugee ohu [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs