Question

I'm implementing a custom update function called update_status for my users controller. I need some help with the routing. what I want to do is update a status that only admins can access. I'm calling the update function via a form helper through the edit function in the users controller. This is my code for the form helper:

<%= form_for @user, :url => url_for(:controller => "users", :action => "update_status"), method: :put do |f| %>
        <%= render "shared/error_messages", object: f.object %>

        <%= f.check_box :admin %>
        <%= f.label :admin %>

        <%= f.check_box :editor %>
        <%= f.label :editor %>

        <%= f.submit "Save Changes", class: "btn btn-primary" %>
    <% end %>

But when I click save changes all I get is this error

enter image description here

I want to route the action so that the user id can be resolved.

Controller action code:

def update_status
  if @user.update_attributes(status_params)
    flash[:success] = "User updated"
    redirect_to @user
  else
    render 'edit'
  end
end

Routes:

Transpub::Application.routes.draw do
resources :users do
  member do
    put 'update_status'
  end
end  
resources :papers 
resources :comments
resources :reviews
resources :sessions, only: [:new, :create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :comments, only: [:create, :destroy]
resources :subject_field, only: [:create, :destroy]
#get "users/new"

root "static_pages#home"
match "/signup", to: "users#new", via: "get"
match "/signin", to: "sessions#new", via: "get"
match "/signout", to: "sessions#destroy", via: "delete"

match "/help", to: "static_pages#help", via: "get"
match "/about", to: "static_pages#about", via: "get"
match "/contact", to: "static_pages#contact", via: "get"

match "/search_papers", to: "papers#index", via: "get"
match "/browse_papers", to: "papers#browse", via: "get"
Was it helpful?

Solution

In your routes files, look for the part that corresponds to the users controller and make sure that you have the following code

resources :users do
  put :update_status, on: :member
end

That will declare the route. Another thing you have to update is the url of the form. Change the url to

form_for @user, :url => [:update_status, @user], html: { method: :put } do |f|
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top