Jquery Rails-发布布尔值(true或False)并更新控制器中的DB
在我的Rails 3应用程序中,我使用jQuery发布了以下内容:Jquery Rails-发布布尔值(true或False)并更新控制器中的DB,jquery,ruby-on-rails,ruby-on-rails-3,Jquery,Ruby On Rails,Ruby On Rails 3,在我的Rails 3应用程序中,我使用jQuery发布了以下内容: http://0.0.0.0:3000/topics/read_updater?&topic_id=101&read=true 在我的控制器中: def read_updater current_user.topics.where(:topic_id => params[:topic_id]).update_all(:read => params[:read]) rende
http://0.0.0.0:3000/topics/read_updater?&topic_id=101&read=true
在我的控制器中:
def read_updater
current_user.topics.where(:topic_id => params[:topic_id]).update_all(:read => params[:read])
render :json => {:status => 'success' }
end
params[:topic_id]工作得很好,但是params[:read]在数据库中插入空值,即使jQuery发布的是true或false。思想?我希望rails用true或false更新数据库
谢谢也许
更新\u all
不是解析传递的字符串吗params[:read]
是一个字符串:
params[:read]
=> "true"
# or
=> "false"
因此,它可能试图将字符串保存到此列,这就是您遇到问题的原因
试试这句话:
current_user.topics.where(:topic_id => params[:topic_id]).update_all(:read => true)
看看它是否有效。如果是,则将:read=>params[:read]
替换为类似:read=>params[:read]==“true”
我还希望您已经检查了params
hash在这个控制器中的外观。如果没有,请填写:
logger.debug params.inspect
在
read\u updater
方法开始时,检查日志是否一切正常。您的意思是params[:topic\u id]
正在工作吗?