gpt4 book ai didi

ruby-on-rails - 事件记录切换方法

转载 作者:行者123 更新时间:2023-12-04 03:41:33 26 4
gpt4 key购买 nike

当用户按下按钮一次时,我想将值从 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

最佳答案

您应该使用 toggle! 方法而不是 toggle。因为没有 ! 的方法不会保存更改。

关于ruby-on-rails - 事件记录切换方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20769518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com