I have a nested model
resources: customers do
resources: readings
end
I am trying to create a new Customer reading. My new action in the Readings Controller is
#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
My view in the readings folder for creating the new reading is
<div class = "page-header">
<h1> New Readings </h1>
</div>
<%= render 'form_reading' %>
And my _form_reading is
<%= 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 %>
However, am troubled, a call to /customers/1/readings/new returns
No route matches {:action=>”show”, :controller=>”readings”}
What am i missing?
In you call to
customer_reading_pathyou are not passing customer_id. You can do it asPlease note that its readings and not reading .