I have a nested model
resources :customer do
resources :readings
end
Customers model has
class Customer < ActiveRecord::Base
attr_accessible :first_name, :last_name, :phase_type, :readings_attributes
has_many :readings
end
Readings Controller has
class Reading < ActiveRecord::Base
attr_accessible :customer_id, :date_of_reading, :reading1, :reading2, :reading3
belongs_to :customer
end
I have written a helper method to determine how to render partials based on the phase_type in the customer’s model however, the helper method is rendered in a form in the readings edit view.
The form in readings/_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_path, :class => 'btn' %>
</div>
<% end %>
The Render_readings_conditionally helper is
module ApplicationHelper
def render_readings_conditionally(form)
if @customer.phase_type == 'Single Phase'
render 'readings/single_phase',f: form
else
render 'readings/three_phase',f: form
end
end
end
The problem i have is i need to determine the type of customer phase type in order to render partials unconditionally however, @customer.phase_type will not work because it is not in the readings model, how can i access it?
I’m not sure what you want to do but maybe this will help in something.
routes.rb for nested resources should be like this:
If you want to access phase_type in @reading
Rails nested resource guide