我使用按钮组作为单选按钮来捕获数据库中布尔字段的用户输入。我有一个带有布尔字段的婚礼模型:rsvp。下面的表格似乎捕获了数据。但是,如果我刷新页面,则不再根据用户的选择切换按钮。我的问题是,即使页面被刷新,或者如果用户注销并再次登录,我如何让按钮保持用户的选择?
楷模
class User < ActiveRecord::Base
has_one :wedding
end
class Wedding < ActiveRecord::Base
belongs_to :user
end
控制器
class WeddingsController < ApplicationController
def new
@wedding = Wedding.new
end
def create
@wedding = current_user.build_wedding(params[:wedding])
if @wedding.save
redirect_to root_path
else
render 'new'
end
end
class StaticPagesController < ApplicationController
def home
@wedding = Wedding.new
end
看法:
主页.html.erb
<%= form_for(@wedding, :remote => true) do |f| %>
<div class="btn-group-wrap">
<div class="btn-group" data-toggle="buttons-radio">
<%= f.button 'Yes', :class => "btn btn-large btn", :value => "1", :name => "wedding[rsvp]" %>
<%= f.button 'No', :class => "btn btn-large btn", :value => "0", :name => "wedding[rsvp]" %>
</div>
</div>
<% end %>