33

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?"
tereško
  • 56,151
  • 24
  • 92
  • 147
MrRuru
  • 1,832
  • 2
  • 19
  • 23

1 Answers1

39

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?")
Abdullah Jibaly
  • 47,520
  • 35
  • 114
  • 192
  • 11
    I didn't know this feature. Having :facebook_uid => nil does not update the attribute, but I used :user => {facebook_uid => 0} instead, and it suits my needs. Thank you! – MrRuru Jan 12 '11 at 16:59
  • 1
    is this method safe & reliable?, i mean is it good to carry POST data as a GET query ? – MhdSyrwan Jul 27 '13 at 08:32
  • Rails handles this as a post/put request.. See the documentation http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to "method: symbol of HTTP verb - This modifier will dynamically create an HTML form and immediately submit the form for processing using the HTTP verb specified." – Yo Ludke Feb 27 '15 at 08:38