[英]How to set status to active in rails3
I am using rails3 and I have a user model. 我正在使用rails3,并且有一个用户模型。 This model has a status column. 该模型有一个状态列。 I am showing admin following table 我正在向管理员显示下表
Mary approve reject
John approve reject
Both approve and reject are links. 批准和拒绝都是链接。 Since clicking on approve will approve user's record it should not be a get request. 由于单击批准将批准用户的记录,因此不应将其作为获取请求。 It should be a post request. 它应该是一个发帖请求。 I was thinking to achieve a post request I should make clicking on approve or reject an ajax call. 我当时想实现一个发布请求,我应该点击批准或拒绝ajax调用。
In the ajax call I would make post call instead of get. 在ajax调用中,我将进行发布而不是获取。
Is this a good strategy? 这是个好策略吗? Anyone has any better suggestion. 任何人都有更好的建议。
Thanks 谢谢
Just pass :method => 'post'
to your link_to
call: 只需将:method => 'post'
传递给您的link_to
调用即可:
<%= link_to 'approve', approve_user_path(user), :method => 'post' %>
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
You're right, it shouldn't be a get request. 没错,这不应该是get请求。 Actually, I think it's neither a post request, because you already have the record and want to change it. 实际上,我认为这既不是发帖请求,因为您已经有了记录并希望更改它。
You could just pass :method => :put
to link_to
. 您可以将:method => :put
传递给link_to
。 Rails will make a JS handler and when the link is clicked, it will create an invisible form with action=PUT
and submit it. Rails将创建一个JS处理程序,并且当单击链接时,它将创建一个带有action=PUT
的不可见表单并提交。
BUT, AJAX is a nice thing too and it's just as hard as setting the method: :remote => true
但是,AJAX也是一件好事,就像设置方法一样困难:remote => true
HTTP POST is used for create, and HTTP PUT is used for update. HTTP POST用于创建,HTTP PUT用于更新。 As such, you should be using doing a PUT (add :method => 'put' to your link_to) since you are updating the user record. 因此,由于您正在更新用户记录,因此您应该使用PUT(在link_to中添加:method =>'put')。 Here's more details: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions 这是更多详细信息: http : //guides.rubyonrails.org/routing.html#crud-verbs-and-actions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.