开发者

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 of select_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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜