我有一个网络应用程序,有三个模型 - 客户,工作和房间。
这三个模型的关联如下:
class Customer < ActiveRecord::Base
has_many :jobs
end
class Job < ActiveRecord::Base
belongs_to :customer
has_many :rooms
end
class Room < ActiveRecord::Base
belongs_to :job
end
我的问题如下:
以下是来自Rooms控制器的新操作和创建操作:
def new
@customer = Customer.find(params[:customer_id])
@job = Job.find(params[:job_id])
@room = @job.rooms.build
end
def create
@customer = Customer.find(params[:customer_id])
@job = Job.find(params[:job_id])
@room = @job.rooms.build(room_params)
respond_to do |format|
if @room.save
format.html { redirect_to job_path(@job), notice: 'Room was successfully created.' }
format.json { render action: 'show', status: :created, location: @room }
else
format.html { render action: 'new' }
format.json { render json: @room.errors, status: :unprocessable_entity }
end
end
end
我收到以下错误:
Couldn't find Customer without an ID
在参数中我看到了以下内容:
{"job_id"=>"3", "format"=>"1"}
我对此处发生的事情感到困惑 - 是否有人知道如何将客户ID从作业视图传递到房间保存,然后使用该客户ID重定向到相应的路径?
如果这很复杂,我很抱歉,我在如何问这个问题几个小时之后一直在努力。如果您需要,请询问澄清。
Routes.rb文件:
resources :customers do
resources :jobs
end
resources :jobs do
resources :rooms
end
devise_for :users
devise_scope :user do
root :to => "devise/sessions#new"
end
新房间表格:
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title center">Add New Room</h3>
</div>
<div class="panel-body">
<%= simple_form_for([@job, @room], html: {class:'form-horizontal'}, wrapper: :horizontal_form) do |f| %>
<div class="form-inputs">
<div class="col-md-12">
<h3 class="bump-right-two"><u>Room Details</u></h3>
</div>
<%= f.input :room_type, collection: ["Kitchen", "Bathroom", "Closet", "Island", "Bar", "Furniture", "Laundry Room", "Office", "Miscellaneous", "Custom Order"], prompt: "Select Room Type" %>
<%= f.input :material, collection: ["Maple", "Cherry", "Oak", "Hickory", "Walnut", "Bamboo", "Thermowrap", "Melamine", "Olon", "Custom Material"], prompt: "Select Material Type" %>
<%= f.input :material_details %>
<%= f.input :exterior_colour %>
<%= f.input :interior_colour, collection: ["White", "Classic Maple", "Dark Truffle", "Caramel", "Stained", "Pre-finished Plywood"], prompt: "Select Interior Colour" %>
<%= f.input :panel_back_style, collection: ["Veneer", "Door Style", "Melamine"], prompt: "Select Panel Back Style" %>
<%= f.input :finished_ends, collection: ["Veneer", "Door Style", "Melamine"], prompt: "Select Finished End Material" %>
<div class="col-md-12">
<hr class="style-one" />
<h3 class="bump-right-two"><u>Door Style Details</u></h3>
</div>
<%= f.input :upper_door_style %>
<%= f.input :lower_door_style %>
<%= f.input :drawer_front_style %>
<div class="col-md-12">
<hr class="style-one" />
<h3 class="bump-right-two"><u>Drawer Box Details</u></h3>
</div>
<%= f.input :drawer_box_style, collection: ["Dovetail", "Melamine", "Hettich"], prompt: "Choose Drawer Box Style" %>
<%= f.input :track_style, collection: ["Tandem", "Hettich", "Accuride"], prompt: "Choose Track Style" %>
<div class="col-md-12">
<hr class="style-one" />
<h3 class="bump-right-two"><u>Counter Top Details</u></h3>
</div>
<%= f.input :counter_top_material, collection: ["Granite", "Quartz", "Corian", "Laminate", "Marble", "Other"], prompt: "Select Counter Top Material" %>
<%= f.input :counter_top_supplier, collection: ["Rosehill", "Floform", "Granite Mountain"], prompt: "Select Counter Top Supplier" %>
<%= f.input :counter_top_colour %>
<%= f.input :counter_top_edge %>
<%= f.input :backsplash, collection: ["Yes", "No"], prompt: "Select Backsplash" %>
<%= f.input :sink_install, collection: ["Yes", "No"], prompt: "Select Sink Install" %>
<div class="col-md-12">
<hr class="style-one" />
<h3 class="bump-right-two"><u>Molding Details</u></h3>
</div>
<%= f.input :closed_to_ceiling, collection: ["Yes", "No"], prompt: "Select Closed to Ceiling" %>
<%= f.input :crown_molding %>
<%= f.input :under_cabinet_molding %>
<div class="col-md-12">
<hr class="style-one" />
<h3 class="bump-right-two"><u>Custom Order Details</u></h3>
</div>
<%= f.input :order_name %>
<%= f.input :order_description %>
</div>
<div class="form-actions">
<%= f.button :submit, "Create New Room", class: "col-md-3 bump-right-two" %>
</div>
<% end %>
</div>
</div>
答案 0 :(得分:2)
rooms
是嵌套在jobs
内的资源:
resources :jobs do
resources :rooms
end
因此,当您导航到RoomsController#new
时,您有一个job_id
参数,因为rooms
嵌套在jobs
中。但是路由中没有customer_id
参数,因为customers
不是嵌套的一部分。由于params[:customer_id]
为nil
,您将收到错误。
由于Job
属于Customer
,您只需通过控制器中的作业获取客户:
def new
@job = Job.find(params[:job_id])
@customer = @job.customer
@room = @job.rooms.build
end