我的编辑操作在视图中的一系列单选按钮。我想填写当前选定字段的值。我设法得到这个工作,但我觉得代码可能是更好的,也或许应该是在模型中。重构和代码放置
控制器:
def edit
@rating = Rating.find(params[:id])
@a,@b,@c,@d,@e,@f,@g,@h,@i,@j = false
if @rating.environ == 1
@a = true
elsif @rating.environ == 2
@b = true
elsif @rating.environ == 3
@c = true
elsif @rating.environ == 4
@d = true
.
.
.
etc.
观点:
1<%= f.radio_button :environ, 1, :checked => @a %>
2<%= f.radio_button :environ, 2, :checked => @b %>
3<%= f.radio_button :environ, 3, :checked => @c %>
.
.
etc..
这大概应该是[代码审查(http://codereview.stackexchange.com/)。 – eykanal