嵌套资源和视图

时间:2012-11-21 12:24:38

标签: ruby-on-rails

我正在编写一个实用程序发票应用程序,而客户有很多读数。读数属于客户。

在我的routes.rb

resources :customers do 
    resources :readings
end

我希望在特定客户的视图中创建一个新的阅读,从而在我的客户控制器中创建

def show
    @customer = Customer.find(params[:id])
    @reading = Reading.new(:customer => @customer)
    respond_to do |format|
      format.html
    end
end

然后在我的客户展示视图中,我添加了添加新客户阅读的表单,如下所示

<%= simple_form_for @customer,@reading do |f| %>
   <%= f.input :customer, :value => @reading.customer.name %>
   <%= f.input :date_of_reading, :as => :date %>
<% end %>

然而,在访问localhost:3000 / customers / 1时,我在Customers#show错误中获得了我添加的用于添加读数的代码段的错误方法。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

问题是表单需要一个数组,但是你传递了两个单独的对象。变化:

<%= simple_form_for @customer, @reading do |f| %>

为:

<%= simple_form_for [@customer, @reading] do |f| %>