我有嵌套资源 - 用户有很多制造商和制造商有很多行。我写了一个before_filter来加载@manufacturer所以它可以通过lines_controller中的其余函数。问题是,当我在行/节目视图上点击编辑时,我遇到了问题。
错误:无法找到ID = manufacturer_id
的制造商77 def load_manufacturer
78 @manufacturer = Manufacturer.find(params[:manufacturer_id])
79 end
所以,我正在与之合作:
应用程序/视图/线/ show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @line.name %>
</p>
<p>
<strong>Manufacturer:</strong>
<%= @line.manufacturer_id %>
</p>
<%= link_to 'Edit', edit_manufacturer_line_path(:manufacturer_id,@line) %> |
<%= link_to 'Back', manufacturer_lines_path(@line.manufacturer_id) %>
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
@line.destroy
respond_to do |format|
format.html { redirect_to manufacturer_lines_url(@manufacturer) }
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
答案 0 :(得分:3)
此:
<%= link_to 'Edit', edit_manufacturer_line_path(:manufacturer_id,@line) %>
应该是
<%= link_to 'Edit', edit_manufacturer_line_path(@manufacturer,@line) %>
正在发生的是将符号:manufacturer_id
更改为字符串并将其传递给路线,以便您的路线看起来像
`/manufacturers/manufacturer_id/lines/###/edit`
何时需要
/manufacturers/###/lines/###/edit