创建了新的嵌套模型

时间:2012-11-23 05:49:15

标签: ruby-on-rails

我有一个嵌套模型

resources: customers do
  resources: readings
end

我正在尝试创建一个新的客户阅读。我在读数控制器中的新动作是

#GET /customers/:customer_id/readings/new
def new
#1st you retrieve the customer
 customer = Customer.find(params[:customer_id])
#2nd you build a new one
 @reading = customer.readings.build
  respond_to do |format|
   format.html #new.html.erb  
  end
end

我在阅读文件夹中创建新阅读的视图是

<div class = "page-header">
   <h1> New Readings </h1>
</div>

<%= render 'form_reading' %>

我的_form_reading是

<%= simple_form_for [@reading.customer, @reading], :html => { :class => 'form-horizontal'    } do |f| %>
<%= render "shared/error_messages", :target => @reading %>
<%= f.input :customer_id %>
<%= f.input :date_of_reading, :as => :date %>
<%= render_readings_conditionally(f) %>
<div class="form-actions">
  <%= f.button :submit, :class => 'btn-primary' %>
  <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
              customer_reading_path, :class => 'btn' %> 
</div>
<% end %>

但是,遇到麻烦,打电话给/ customers / 1 /读取/新回报

没有路线匹配{:action =&gt;“show”,:controller =&gt;“readings”}

我缺少什么?

2 个答案:

答案 0 :(得分:0)

customer_reading_path (:customer_id => <Pass customer ID here>)

答案 1 :(得分:0)

在致电customer_reading_path时,您没有通过customer_id。你可以这样做

customer_readings_path(@reading.customer)

请注意读数而非读数

相关问题