我有一个嵌套的模型设置,用户有多个制造商,制造商有多条线。当我尝试从行索引中删除该行的实例时,我得到一条错误的路由消息。我不确定我做错了什么,但它可能很简单,希望有人可以在这里省点我的心痛。我已经阅读了hartl的东西,我对嵌套的资源和路由仍然非常新。
=== MODELS ===
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :manufacturers
end
==
class Manufacturer < ActiveRecord::Base
belongs_to :user
has_many :lines
end
==
class Line < ActiveRecord::Base
belongs_to :manufacturer
end
==
lines_controller.rb
class LinesController < ApplicationController
before_action :set_line, only: [:show, :edit, :update, :destroy]
before_filter :load_manufacturer
# GET /lines
# GET /lines.json
def index
@lines = Line.all
end
# GET /lines/1
# GET /lines/1.json
def show
end
# GET /lines/new
def new
@manufacturer = Manufacturer.find(params[:manufacturer_id])
@line = @manufacturer.lines.build
end
# GET /lines/1/edit
def edit
end
# POST /lines
# POST /lines.json
def create
@line = @manufacturer.lines.build(line_params)
respond_to do |format|
if @line.save
format.html { redirect_to manufacturer_line_path(@manufacturer, @line), notice: 'Line was successfully created.' }
format.json { render action: 'show', status: :created, location: @line }
else
format.html { render action: 'new' }
format.json { render json: @line.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /lines/1
# PATCH/PUT /lines/1.json
def update
respond_to do |format|
if @line.update(line_params)
format.html { redirect_to manufacturer_line_path(@manufacturer, @line), notice: 'Line was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @line.errors, status: :unprocessable_entity }
end
end
end
# DELETE /lines/1
# DELETE /lines/1.json
def destroy
@manufacturer.line.destroy
respond_to do |format|
format.html { redirect_to lines_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line
@line = Line.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def line_params
params.require(:line).permit(:name, :manufacturer_id)
end
def load_manufacturer
@manufacturer = Manufacturer.find(params[:manufacturer_id])
end
end
=====
Prefix Verb URI Pattern Controller#Action
manufacturer_lines GET /manufacturers/:manufacturer_id/lines(.:format) lines#index
POST /manufacturers/:manufacturer_id/lines(.:format) lines#create
new_manufacturer_line GET /manufacturers/:manufacturer_id/lines/new(.:format) lines#new
edit_manufacturer_line GET /manufacturers/:manufacturer_id/lines/:id/edit(.:format) lines#edit
manufacturer_line GET /manufacturers/:manufacturer_id/lines/:id(.:format) lines#show
PATCH /manufacturers/:manufacturer_id/lines/:id(.:format) lines#update
PUT /manufacturers/:manufacturer_id/lines/:id(.:format) lines#update
DELETE /manufacturers/:manufacturer_id/lines/:id(.:format) lines#destroy
manufacturers GET /manufacturers(.:format) manufacturers#index
POST /manufacturers(.:format) manufacturers#create
new_manufacturer GET /manufacturers/new(.:format) manufacturers#new
edit_manufacturer GET /manufacturers/:id/edit(.:format) manufacturers#edit
manufacturer GET /manufacturers/:id(.:format) manufacturers#show
PATCH /manufacturers/:id(.:format) manufacturers#update
PUT /manufacturers/:id(.:format) manufacturers#update
DELETE /manufacturers/:id(.:format) manufacturers#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / pages#home
about GET /about(.:format) pages#about
GET /users/:id(.:format) users#show
all_users GET /users(.:format) users#index
public_profile GET /users/:name(.:format) users#show
==
应用程序/视图/线/ index.html.erb
<h1>Listing lines</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Manufacturer</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @lines.each do |line| %>
<tr>
<td><%= line.name %></td>
<td><%= line.manufacturer_id %></td>
<td><%= link_to 'Show', manufacturer_lines_path(line.manufacturer, line) %></td>
<td><%= link_to 'Edit', edit_manufacturer_line_path(line.manufacturer, line) %></td>
<td><%= link_to 'Destroy', manufacturer_lines_path, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
答案 0 :(得分:11)
这是试图使用错误的路由助手:
<td><%= link_to 'Destroy', manufacturer_lines_path, method: :delete, data: { confirm: 'Are you sure?' } %></td>
应该是:
<td><%= link_to 'Destroy', manufacturer_line_path(line.manufacturer, line), method: :delete, data: { confirm: 'Are you sure?' } %></td>
manufacturer_lines_path
正在做的是使用路由助手来生成/manufacturers/1/lines
这样的路径,这是完全有效的。 你没有这条路线。您可能只有一个用于DELETE
而另一个用于rake routes
,但不适用于GET
。
所以解决方案是使用正确的路径助手POST
,它使用DELETE
个对象和manufacturer_line_path
对象,并从中生成一个路由,例如{{1} }。再次,查看Manufacturer
的输出,您将看到为此路径定义了几条路由。一个Line
,一个/manufacturers/1/lines/2
,一个rake routes
和一个GET
。
当您提出PUT
请求时,最后一条路线将会匹配,而您的应用程序的路由器会将其路由到PATCH
&#39; s { {1}}操作,在参数中传递DELETE
和DELETE /manufacturers/1/lines/2
。