路由错误,删除嵌套对象

时间:2017-04-30 06:15:24

标签: ruby-on-rails ruby model-view-controller

我创建了一个应用程序,用户可以保存他的最大重量提升。我在创建删除方法时遇到错误。

我的RM控制器:

class RmsController < ApplicationController

  def destroy
    @user = User.find(params[:user_id])
    @rm.find(params[:id]).destroy
    flash[:success] = "Repeticion maxima eliminada"
    redirect_to user_rms_path
  end
end

rm的索引(我试图删除的地方)

<ul class="rms">

  <% @rms.each do |rm| %>
  <li>
    <%= p rm.content %>
    <%= p '-' %>
    <%= p rm.quantity  %>
    <% if !current_user?(@user) %>
      | <%= link_to "delete", @rm, method: :delete, 
                          data: { confirm: "You sure?" } %>
                          <% end %>
  </li>
  <% end %>

我得到了下一个错误:

  

路由错误没有路由匹配[DELETE]“/ users / 1 / rms”

这是我的config routes.rb:

Rails.application.routes.draw do

  root 'static_pages#home'
  get    '/signup',               to: 'users#new'
  get    '/contact',              to: 'static_pages#contact'
  get    '/about',                to: 'static_pages#about'
  get    '/login',                to: 'sessions#new'
  post   '/login',                to: 'sessions#create'
  delete '/logout',               to: 'sessions#destroy'


  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
  resources :users do
    resources :rms 
  end
  resources :trainings do
    resources :bookings
  end
end

rake routes命令:

Prefix Verb   URI Pattern                                         Controller#Action
                   root GET    /                                                   static_pages#home
                 signup GET    /signup(.:format)                                   users#new
                contact GET    /contact(.:format)                                  static_pages#contact
                  about GET    /about(.:format)                                    static_pages#about
                  login GET    /login(.:format)                                    sessions#new
                        POST   /login(.:format)                                    sessions#create
                 logout DELETE /logout(.:format)                                   sessions#destroy
edit_account_activation GET    /account_activations/:id/edit(.:format)             account_activations#edit
        password_resets POST   /password_resets(.:format)                          password_resets#create
     new_password_reset GET    /password_resets/new(.:format)                      password_resets#new
    edit_password_reset GET    /password_resets/:id/edit(.:format)                 password_resets#edit
         password_reset PATCH  /password_resets/:id(.:format)                      password_resets#update
                        PUT    /password_resets/:id(.:format)                      password_resets#update
               user_rms GET    /users/:user_id/rms(.:format)                       rms#index
                        POST   /users/:user_id/rms(.:format)                       rms#create
            new_user_rm GET    /users/:user_id/rms/new(.:format)                   rms#new
           edit_user_rm GET    /users/:user_id/rms/:id/edit(.:format)              rms#edit
                user_rm GET    /users/:user_id/rms/:id(.:format)                   rms#show
                        PATCH  /users/:user_id/rms/:id(.:format)                   rms#update
                        PUT    /users/:user_id/rms/:id(.:format)                   rms#update
                        DELETE /users/:user_id/rms/:id(.:format)                   rms#destroy
                  users GET    /users(.:format)                                    users#index
                        POST   /users(.:format)                                    users#create
               new_user GET    /users/new(.:format)                                users#new
              edit_user GET    /users/:id/edit(.:format)                           users#edit
                   user GET    /users/:id(.:format)                                users#show
                        PATCH  /users/:id(.:format)                                users#update
                        PUT    /users/:id(.:format)                                users#update
                        DELETE /users/:id(.:format)                                users#destroy
      training_bookings GET    /trainings/:training_id/bookings(.:format)          bookings#index
                        POST   /trainings/:training_id/bookings(.:format)          bookings#create
   new_training_booking GET    /trainings/:training_id/bookings/new(.:format)      bookings#new
  edit_training_booking GET    /trainings/:training_id/bookings/:id/edit(.:format) bookings#edit
       training_booking GET    /trainings/:training_id/bookings/:id(.:format)      bookings#show
                        PATCH  /trainings/:training_id/bookings/:id(.:format)      bookings#update
                        PUT    /trainings/:training_id/bookings/:id(.:format)      bookings#update
                        DELETE /trainings/:training_id/bookings/:id(.:format)      bookings#destroy
              trainings GET    /trainings(.:format)                                trainings#index
                        POST   /trainings(.:format)                                trainings#create
           new_training GET    /trainings/new(.:format)                            trainings#new
          edit_training GET    /trainings/:id/edit(.:format)                       trainings#edit
               training GET    /trainings/:id(.:format)                            trainings#show
                        PATCH  /trainings/:id(.:format)                            trainings#update
                        PUT    /trainings/:id(.:format)                            trainings#update
                        DELETE /trainings/:id(.:format)                            trainings#destroy

我想知道为什么我会收到此错误以及我如何修复它,因此只要用户需要,就可以删除Max rep。

1 个答案:

答案 0 :(得分:0)

您需要在link_to标记中传递正确的路由。运行RmsController的rake路由并找出destroy path(例如:user_rms_path)。

<% unless current_user?(@user) %>
      | <%= link_to "delete", [@user, @rm], method: :delete, 
                          data: { confirm: "You sure?" } %>
                          <% end %>

尝试一下它会起作用。