rails的自定义更新功能路由

时间:2014-04-23 14:49:30

标签: ruby-on-rails rails-routing

我为我的用户控制器实现了一个名为update_status的自定义更新功能。我需要一些路由帮助。我想要做的是更新只有管理员才能访问的状态。我通过用户控制器中的编辑功能通过表单助手调用更新功能。这是表单助手的代码:

<%= 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 %>

但是当我点击保存更改时,我得到的就是这个错误

enter image description here

我想路由操作,以便可以解析用户ID。

控制器操作代码:

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

路线:

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"

1 个答案:

答案 0 :(得分:1)

在路线文件中,查找与用户控制器对应的部分,并确保您拥有以下代码

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

那将宣布路线。您需要更新的另一件事是表单的URL。将网址更改为

form_for @user, :url => [:update_status, @user], html: { method: :put } do |f|