Rails - 更新单个属性:与自定义操作或带有隐藏字段的表单链接?
假设我有一个 User 模型,其中 facebook_uid 字段对应于用户的 facebook id。
我想允许用户取消其 Facebook 帐户的链接。这样做,我需要将此属性设置为零。
我目前看到有两种方法可以做到这
一点第一种方法:创建自定义操作并链接到它
# app/controllers/users_controller.rb
def unlink_facebook_account
@user = User.find params[:id]
# Authorization checks go here
@user.facebook_uid = nil
@user.save
# Redirection go here
end
# config/routes.rb
ressources :users do
get 'unlink_fb', :on => :member, :as => unlink_fb
end
# in a view
= link_to "Unlink your facebook account", unlink_fb_path(@user)
第二种方法:创建现有更新操作的表单
# app/views/user/_unlink_fb_form.html.haml
= form_for @user, :method => "post" do |f|
= f.hidden_field :facebook_uid, :value => nil
= f.submit "Unlink Facebook account"
我不是这两种方法的忠实粉丝。
在第一个中,我必须为更新控制器已经可以执行的操作添加新操作。
在第二个中,如果不自定义更新操作,我无法将 facebook_uid 设置为 nil,并且如果不添加一些 javascript,我就无法使用链接代替按钮。
不过,您会推荐什么作为这种情况下最好、最优雅的解决方案?我错过了第三种选择吗?
解决方案(Abdullah Jibaly 建议)
使用更新操作的链接,并将要更新的属性作为参数;并处理属性设置为0时的情况
= link_to "Unlink your facebook account",
user_path(@user,
:user => { :facebook_uid => 0}),
:method => :put,
:confirm => "Are you sure?"
Let's say I have a User model, with a facebook_uid field corresponding to the user's facebook id.
I want to allow the user to unlink his facebook account. Do do so, I need to set this attribute to nil.
I currently see 2 ways of doing this
First way : create a custom action and link to it
# app/controllers/users_controller.rb
def unlink_facebook_account
@user = User.find params[:id]
# Authorization checks go here
@user.facebook_uid = nil
@user.save
# Redirection go here
end
# config/routes.rb
ressources :users do
get 'unlink_fb', :on => :member, :as => unlink_fb
end
# in a view
= link_to "Unlink your facebook account", unlink_fb_path(@user)
Second way : create a form to the existing update action
# app/views/user/_unlink_fb_form.html.haml
= form_for @user, :method => "post" do |f|
= f.hidden_field :facebook_uid, :value => nil
= f.submit "Unlink Facebook account"
I'm not a big fan of either way.
In the first one, I have to add a new action for something that the update controller already can do.
In the second one, I cannot set the facebook_uid to nil without customizing the update action, and I cannot have a link instead of a button without adding some javascript.
Still, what would you recommend as the best and most elegant solution for this context? Did I miss a third alternative?
Solution (suggested by Abdullah Jibaly)
Use a link to the update action, with the attribute to update as parameters ; and handle the case when the attribute is set to 0
= link_to "Unlink your facebook account",
user_path(@user,
:user => { :facebook_uid => 0}),
:method => :put,
:confirm => "Are you sure?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定您的路线是什么样子,但如果您有更新操作,它应该与 link_to 一起使用。如果您使用链接,请确保使用 PUT 或 POST 方法(最好是 PUT,因为它是更新):
Not sure what your routes look like but if you have an update action it should work with the link_to. If you go with the link make sure you use a method of PUT or POST (ideally PUT because it's an update):