我的編輯操作在視圖中的一系列單選按鈕。我想填寫當前選定字段的值。我設法得到這個工作,但我覺得代碼可能是更好的,也或許應該是在模型中。重構和代碼放置
控制器:
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