Ruby on Rails 3: Playing with Select on Input Page
I created an input page with a textbox in every field, and it's working.
I changed one field in the select box with the following code:
<div class="field">
<%= f.label :position %><br />
<%= select_tag "position", options_for_select(%w{ mainpost bannerpost2 minipost1 minipost2 minipost3 }) %>
</div>
When I edit my ne开发者_运维技巧w post, I change the select_tag
value into bannerpost2
, and then I update my post, but the field of position
doesn't change into bannerpost2
.
Also, when I edit my post, the f.select
doesn't change automatically into it's value.
You need to use
f.select
instead ofselect_tag
, because rails automatically append table name to each ids, f.select will be "post_position" if your table is "Posts", but select_tag will be just "position", so data won't get stored in db.For previous value saved in db, should pass it as an arg to options_for_select
In case, if your table name is "Posts
", pass @post.position
f.select :position,
options_for_select(%w{ mainpost bannerpost2 minipost1 minipost2 minipost3 },
@post.position)
精彩评论