![](/img/trans.png)
[英]How to render a “Edit” form partial in the “Show” page of another model in Rails
[英]Rails 3: Loading a form Partial into an edit view of another model/controller
我有兩個單獨的模型/控制器。 一個叫做“商人”,另一個叫做“ CompanyLocations”。 我需要能夠在同一表格上添加一個新商人和至少一個位置。 使用按鈕可以在需要時添加更多位置。
使用樣本數據,我使用位置數據填充了30個商家中的前5個。 現在,“顯示商家”視圖可以正常運行,它顯示了下方的商家和公司位置。
我的問題是在新的/編輯頁面上。 我似乎無法將另一個位置放到與商家ID相關聯的數據庫中。
我收到的錯誤是:嘗試在商家編輯頁面上添加公司位置后,“未定義方法'company_locations'為nil:NilClass”。
當我嘗試添加新商人時,也會出現錯誤。 並且添加按鈕會顯示兩次,一次是針對新商家,一次是針對添加地點。
在我的模型中,我有商人.rb
has_many :company_locations, :dependent => :destroy
company_location.rb
belongs_to :merchant
這是我的文件:controllers / merchant_controller.rb
def new
@merchant = Merchant.new
@company_location = @merchant.company_locations.new
end
def edit
@merchant = Merchant.find(params[:id])
@company_location = @merchant.company_locations.new
end
控制器/ company_locations_controller.rb
def create
@company_location = @merchant.company_locations.build(params[:company_location])
if @company_location.save
redirect_to 'merchants/show', :flash => { :success => "Company Location Added" }
else
redirect_to 'merchants/index', :flash => { :error => "Location not saved" }
end
end
def update
end
意見/商家/ new.html.erb
<h1>New merchant</h1>
<%= render 'form' %>
<%= render 'company_locations/form' %>
<%= link_to 'Cancel', merchants_path %>
意見/商家/ _form.html
<%= form_for(@merchant) do |f| %>
<% if @merchant.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@merchant.errors.count, "error") %> prohibited this merchant from being saved:</h2>
<ul>
<% @merchant.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :company_name %><br />
<%= f.text_field :company_name %>
</div>
<div class="field">
<%= f.label :fanpage_url %><br />
<%= f.text_field :fanpage_url %>
</div>
<div class="field">
<%= f.label :twitter_id %><br />
<%= f.text_field :twitter_id %>
</div>
<div class="field">
<%= f.label :website_url %><br />
<%= f.text_field :website_url %>
</div>
<div class="field">
<%= f.label :contact_email %><br />
<%= f.text_field :contact_email %>
</div>
<div class="field">
<%= f.label :active %><br />
<%= f.check_box :active %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
視圖/ company_locations / _form.html.erb
<%= form_for @company_location do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :address1 %><br />
<%= f.text_field :address1 %>
</div>
<div class="field">
<%= f.label :address2 %><br />
<%= f.text_field :address2 %>
</div>
<div class="field">
<%= f.label :city %><br />
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :state %><br />
<%= f.text_field :state %>
</div>
<div class="field">
<%= f.label :zip %><br />
<%= f.text_field :zip %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :fax %><br />
<%= f.text_field :fax %>
</div>
<div class="actions">
<%= f.submit "Add Location" %>
</div>
<% end %>
如果我正確理解了您的示例,我認為基本問題是您正在為兩個單獨的模型在視圖上創建兩個單獨的表單。 Rails(和大多數最新框架)的優點在於可以使用“嵌套表單”。 這里有一個不錯的概述(盡管有些過時):
http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
您可以在此處查看有關該主題的出色的Railscast:
http://railscasts.com/episodes/196-nested-model-form-part-1
一般而言,您應該能夠創建一種表格,該表格可以一口氣編輯商戶及其關聯的CompanyLocation。 可能需要花費一些時間,但是這大大簡化了控制器和未來的開發。 有幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.