Rails - 更新单个属性:与自定义操作或带有隐藏字段的表单链接?

发布于 2024-10-12 00:06:05 字数 1337 浏览 2 评论 0原文

假设我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2024-10-19 00:06:05

不确定您的路线是什么样子,但如果您有更新操作,它应该与 link_to 一起使用。如果您使用链接,请确保使用 PUT 或 POST 方法(最好是 PUT,因为它是更新):

link_to("Unlink your facebook account", user_path(@user, :facebook_uid => nil), :method => :put, :confirm => "Are you sure?")

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):

link_to("Unlink your facebook account", user_path(@user, :facebook_uid => nil), :method => :put, :confirm => "Are you sure?")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文