当用户按下按钮一次时,我想将值从false更改为true。如果用户再次按下按钮,则值应从true更改为false。
我尝试使用下面的代码,但失败了。我该如何解决这个问题?
☆show.html.erb(group_messages_controller)
<div class="bottom">
<%= link_to"解決!", {:controller =>'group_messages', :action => 'kaiketsu', :id=>@group_message.id}, class: "btn btn-medium btn-default" %></b>
</div>
☆group_messages_controller
def show
if !checklogin? then return end
@group_message = GroupMessage.find(params[:id])
@isme = me? @group_message
@group_message_comment = GroupMessageComment.new
@group_message_comment = GroupMessage.find(params[:id]).group_message_comments.order("created_at asc").build
@isme_cmt = me? @group_message_comment
@gmsc_member_id = session[:user_id]
@gm_in = GroupMessage.where(:group_id => @group_message.group_id).order("created_at desc").paginate(:page => params[:page], :per_page => 6)
@gm_in_one = @gm_in.map{|gm| gm}
respond_to do |format|
format.html # show.html.erb
format.json { render json: @group_message }
end
end
def kaiketsu
@group_message= GroupMessage.find(params[:id])
@group_message.toggle(:kaiketsu)
@group_message.update_attributes(params[:group_message])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @group_message }
end
end
☆schema.rb
create_table "group_messages", :force => true do |t|
t.integer "member_id", :null => false
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "group_id", :null => false
t.integer "page"
t.integer "line"
t.string "name"
t.string "username"
t.boolean "kaiketsu", :default => false
end
答案 0 :(得分:9)
您应该使用toggle!
方法代替toggle
。因为没有!
的方法无法保存更改。