Joe Essey
Joe Essey

Reputation: 3538

Method is not being called, but is defined in controller and model

I have a method to toggle a user between admin or not-admin. When I click the link I am placed back in the User index with the following in my address bar: http://localhost:3000/users?id=1&method=toggle_admin. As far as I can tell, I have the appropriate code to run the method. Can anyone see a mistake?

Here's the view link_to method:

<%= link_to 'Toggle Admin', { :controller => :users, :method => :toggle_admin, :id => user.id} %>

Here's the routes.rb statement:

match 'users/:id/toggle_admin' => 'users#toggle_admin'

The controller method:

def toggle_admin
    @user = User.find(params[:id])

    User.toggle_admin(@user)

    respond_to do |format|
      format.html { redirect_to @users }
    end

end

The model method:

def toggle_admin(user)
  if user.is_admin.nil or user.is_admin = ''
     user.is_admin = false
  end
  user.toggle is_admin

  user.save
end

Upvotes: 0

Views: 462

Answers (1)

revolver
revolver

Reputation: 2405

Trying using :action instead of :method. :method should be used with HTTP verb (i.e GET, PUT and so on)

<%= link_to 'Toggle Admin', { :controller => :users, :action => :toggle_admin, :id => user.id} %>

Upvotes: 2

Related Questions