调用嵌套模型视图

时间:2012-11-28 21:14:51

标签: ruby-on-rails nested-forms

我有一个嵌套模型

resources :customers do 
 resources :readings
end

我现在想从客户的展示视图访问我的../customerid/readings/new视图。

如果在客户展示视图中,如何使用按钮调用新的读数视图?

客户的show.html

<%- model_class = Customer -%>
<div class="page-header">
<h1><%=t '.title', :default => model_class.model_name.human %></h1>
</div>

<dl class="dl-horizontal">
<dt><strong><%= model_class.human_attribute_name(:name) %>:</strong></dt>
<dd><%= @customer.name %></dd>
<dt><strong><%= model_class.human_attribute_name(:customer_currency) %>:</strong></dt>
<dd><%= @customer.customer_currency %></dd>
<dt><strong><%= model_class.human_attribute_name(:payment_terms) %>:</strong></dt>
<dd><%= @customer.payment_terms %></dd>
<dt><strong><%= model_class.human_attribute_name(:phase_type) %>:</strong></dt>
<dd><%= @customer.phase_type %></dd>
<dt><strong><%= model_class.human_attribute_name(:billing_address) %>:</strong></dt>
<dd><%= @customer.billing_address %></dd>
<dt><strong><%= model_class.human_attribute_name(:first_name) %>:</strong></dt>
<dd><%= @customer.first_name %></dd>
<dt><strong><%= model_class.human_attribute_name(:last_name) %>:</strong></dt>
<dd><%= @customer.last_name %></dd>
<dt><strong><%= model_class.human_attribute_name(:mobile) %>:</strong></dt>
<dd><%= @customer.mobile %></dd>
<dt><strong><%= model_class.human_attribute_name(:email) %>:</strong></dt>
<dd><%= @customer.email %></dd>
</dl>


<div class="form-actions">
<%= link_to t('.back', :default => t("helpers.links.back")),
          customers_path, :class => 'btn'  %>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
          edit_customer_path(@customer), :class => 'btn' %>


<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
          customer_path(@customer),
          :method => 'delete',
          :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm",   :default => 'Are you sure?')) },
          :class => 'btn btn-danger' %>
</div>

2 个答案:

答案 0 :(得分:2)

我及时解决了

  <%= link_to 'Readings', customer_readings_path(@customer) %> |

答案 1 :(得分:1)

根据路由上的Ruby on Rails指南的"2.7 Nested Resources" Chapter,要获取以下URL /customers/:customer_id/readings/new,请使用:

customer_readings_path(@customer) # Generate the path for new Reading which belongs to @customer