Ruby on rails Rails 3:将表单部分加载到另一个模型/控制器的编辑视图中
我有两个独立的模型/控制器。一个叫“商人”,另一个叫“公司定位”。我需要能够添加一个新的商户和至少一个位置在同一表格。按钮,允许我添加更多的位置,如果需要 使用示例数据,我用位置数据填充了30个商家中的前5个。现在,Show Merchant视图工作正常,它显示了下面的商家和公司位置 我的问题在新/编辑页面上。我似乎无法将另一个位置放入与商户id关联的DB中 我得到的错误是:在我尝试在商户编辑页面上添加公司位置后,“nil:NilClass的未定义方法`company_locations'” 当我尝试添加一个新商户时,我也会遇到一个错误。“添加”按钮会显示两次,一次用于新商户,一次用于添加位置 在我的模型中,我有 商人.rbRuby on rails Rails 3:将表单部分加载到另一个模型/控制器的编辑视图中,ruby-on-rails,web-applications,ruby-on-rails-3,Ruby On Rails,Web Applications,Ruby On Rails 3,我有两个独立的模型/控制器。一个叫“商人”,另一个叫“公司定位”。我需要能够添加一个新的商户和至少一个位置在同一表格。按钮,允许我添加更多的位置,如果需要 使用示例数据,我用位置数据填充了30个商家中的前5个。现在,Show Merchant视图工作正常,它显示了下面的商家和公司位置 我的问题在新/编辑页面上。我似乎无法将另一个位置放入与商户id关联的DB中 我得到的错误是:在我尝试在商户编辑页面上添加公司位置后,“nil:NilClass的未定义方法`company_locations'” 当
has_many :company_locations, :dependent => :destroy
company_location.rb
belongs_to :merchant
这是我的档案:
控制器/商户控制器.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
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
控制器/公司位置控制器.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
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 %>
新商户
views/merchants/_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\u locations/\u 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 %>
f、 对象%>
如果我正确理解了您的示例,我认为基本问题是您在视图上为两个单独的模型创建了两个单独的表单。Rails(以及大多数当前框架)的优点在于能够使用“嵌套表单”。这里有一个很好的概述(虽然有点过时):
你可以在这里看到一个很棒的Railscast主题:
一般来说,您应该能够创建一个表单,可以一次性编辑商户及其关联公司的位置。这可能需要花费一点时间,但它大大简化了控制器和未来的开发。这有用吗?Nuby,这正是我需要的。谢谢