我有一个嵌套模型
resources :customer do
resources :readings
end
客户模型有
class Customer < ActiveRecord::Base
attr_accessible :first_name, :last_name, :phase_type, :readings_attributes
has_many :readings
end
读数控制器有
class Reading < ActiveRecord::Base
attr_accessible :customer_id, :date_of_reading, :reading1, :reading2, :reading3
belongs_to :customer
end
我已经编写了一个辅助方法来确定如何根据客户模型中的 phase_type 呈现部分,但是,辅助方法在读数编辑视图中以表单形式呈现。
readings/_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_path, :class => 'btn' %>
</div>
<% end %>
Render_readings_conditionally 助手是
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
我遇到的问题是我需要确定客户阶段类型的类型才能无条件地渲染部分但是,@customer.phase_type 将不起作用,因为它不在读数模型中,我该如何访问它?