没有路由匹配[POST]“ / users / 1”

时间:2019-03-14 02:17:44

标签: ruby-on-rails

在视图中,我有一个link_to,我想用它来调用控制器中的方法来更新模型。

这是查看代码:

  <tbody>
    <% @users.each do |user| %>
      <tr data-user-id="<%= user.id %>">
        <td><%= user.email %></td>
        <td><%= link_to 'Show', user %></td>
        <td><%= link_to 'Edit', edit_user_path(user) %></td>
        <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <td><%= link_to 'Admin', user, method: :admin %></td>      

      </tr>
    <% end %>
  </tbody>

此链接是不起作用的特定链接,因为它不是用户控制器中的默认方法

<td><%= link_to 'Admin', user, method: :admin %></td>  

我的控制器中的admin方法如下

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

    if @user.isadmin?

      redirect_to '/users', alert: 'User was updated to admin.'

      else
        redirect_to '/users', alert: 'User is already an admin.'
      end

  end

在route.rb文件中没有定义任何路由,因为我不确定如何正确定义它。视图是否存在问题,或者没有路线存在问题。我以为可以在链接中指定控制器和动作,但这似乎不起作用。

这是控制台输出

Started POST "/users/5" for 192.168.1.1 at 2019-03-13 22:25:50 -0400

ActionController::RoutingError (No route matches [POST] "/users/5"):

actionpack (5.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:22:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.2) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.2) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.2) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.2) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.2) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.6) lib/rack/method_override.rb:22:in `call'
rack (2.0.6) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.6) lib/rack/sendfile.rb:111:in `call'
railties (5.2.2) lib/rails/engine.rb:524:in `call'
puma (3.12.0) lib/puma/configuration.rb:225:in `call'
puma (3.12.0) lib/puma/server.rb:658:in `handle_request'
puma (3.12.0) lib/puma/server.rb:472:in `process_client'
puma (3.12.0) lib/puma/server.rb:332:in `block in run'
puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'

routes.rb

  resources :users
  post 'users/:id' =>  'users#admin'

3 个答案:

答案 0 :(得分:1)

这应该创建您需要的路线。

post 'users/:id/admin' =>  'users#admin'

您的链接看起来像

<% = link_to 'Admin', users_admin_path(user) %> # or whatever rails names your route

答案 1 :(得分:0)

尝试将链接更改为:

 <%= link_to "Admin", admin_path(user) %>

您的路线:

 post 'admin', to: 'admin#users', as: :admin

答案 2 :(得分:0)

在视图文件中,您的管理员链接不正确。方法应该是:delete,:post等,不是:admin

<td><%= link_to 'Admin', user, method: :admin %></td> 

解决方法-

解决方案#1

# In view
<td><%= link_to 'Admin', admin_user_path(user) %></td> 

路线应为

resources :users do
 member do
  get 'admin'
 end
end

解决方案2

# In view
<td><%= link_to 'Admin', admin_user_path(user), method: :post %></td> 

路线应为

resources :users do
 member do
  post 'admin'
 end
end