Reputation: 5
I have a rails form helper that looks like this:
<%= text_field :username,nil, {:placeholder=>"new username", :value=>nil} %>
When I look at the HTML generated by this, it looks like:
input id="username_" name="username[]" placeholder="new username" size="30" type="text"
(HTML <> removed)
This makes accessing the username attribute troubling. I do:
@user.username = [:username]
Params looks like
{"utf8"=>"✓", "authenticity_token"=>"...", "username"=>["newusername"], "commit"=>"Save", "id"=>"10"}
And @user.username gets set to (escaped) ["newusername"] - it gets set to "--- - newusername"
Can't figure out how to get rid of the [] around the form value! What should I do?
EDIT: Entire form as generated by rails: http://tny.cz/0d5a9397
Upvotes: 0
Views: 575
Reputation: 29599
you should use text_field_tag
<%= text_field_tag :username, nil, :placeholder => "new username" %>
using text_field
somehow expects you to work on a resource.
text_field :user, :username
You can still use text_field but you have to explicitly set the name
text_field :username, nil, name: 'username'
What I didn't know was if you pass nil
as the second argument, it will set that field as a collective tag so I learned something new today.
Upvotes: 1