Ruby on rails 显示嵌套模型
我的嵌套模型表单现在正在工作,但在视图中显示数据时遇到问题。如何显示具有一对多关系的嵌套模型数据?任何帮助都将不胜感激 这是我的表单和控制器:Ruby on rails 显示嵌套模型,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我的嵌套模型表单现在正在工作,但在视图中显示数据时遇到问题。如何显示具有一对多关系的嵌套模型数据?任何帮助都将不胜感激 这是我的表单和控制器: <%= form_for @account do |f| %> <%= f.label :account_type %><br /> <%= f.text_field :account_type %><br /> <%= f.fields_for :organizations
<%= form_for @account do |f| %>
<%= f.label :account_type %><br />
<%= f.text_field :account_type %><br />
<%= f.fields_for :organizations do |builder| %>
<%= builder.label :name %><br />
<%= builder.text_field :name %><br />
<%= builder.label :website %><br />
<%= builder.text_field :website %><br />
<%= builder.fields_for :locations do |lb| %>
<%= lb.label :phone %><br />
<%= lb.text_field :phone %><br />
<%= lb.label :toll_free_phone %><br />
<%= lb.text_field :toll_free_phone %><br />
<%= lb.label :fax %><br />
<%= lb.text_field :fax %><br />
<%= lb.fields_for :addresses do |ab| %>
<%= ab.label :address1 %><br />
<%= ab.text_field :address1 %><br />
<%= ab.label :address2 %><br />
<%= ab.text_field :address2 %><br />
<%= ab.label :city %><br />
<%= ab.text_field :city %><br />
<%= ab.label :state %><br />
<%= ab.text_field :state %><br />
<%= ab.label :zip %><br />
<%= ab.text_field :zip %><br />
<% end %>
<% end %>
<% end %>
<%= f.submit "Add account" %>
<% end %>
class AccountsController < ApplicationController
def show
@account = Account.find(params[:id])
@organization = @account.organizations
end
def new
@account = Account.new
organization = @account.organizations.build
location = organization.locations.build
location.addresses.build
@header = "Create account"
end
def create
@account = Account.new(params[:account])
if @account.save
flash[:success] = "Account added successfully"
render 'show'
else
render 'new'
end
end
end
类AccountsController<应用程序控制器
def秀
@account=account.find(参数[:id])
@组织=@account.organizations
结束
def新
@account=account.new
组织=@account.organizations.build
位置=organization.locations.build
location.addresses.build
@header=“创建帐户”
结束
def创建
@account=account.new(参数[:account])
如果@account.save
flash[:success]=“已成功添加帐户”
渲染“显示”
其他的
呈现“新”
结束
结束
结束
通常,当存在一对多关系时,如何在视图中引用嵌套模型数据?我是否需要使用类似“where子句”的方法指定子级
下面是一个简单的示例show.html.erb,我试图显示我刚刚创建的组织的名称。它不起作用
<h1><%= @organization.name %></h1>
使用上述表单创建帐户后的“显示”操作将导致此错误:
NoMethodError in Accounts#create
Showing C:/Documents and Settings/Corey Quillen/My
Documents/rails_projects/shop_manager/app/views/accounts/show.html.erb where line #1
raised:
undefined method `name' for nil:NilClass
Extracted source (around line #1):
1: <h1><%= @organization.name %></h1>
Rails.root: C:/Documents and Settings/Corey Quillen/My
Documents/rails_projects/shop_manager
Application Trace | Framework Trace | Full Trace
app/views/accounts/show.html.erb:1:in
`_app_views_accounts_show_html_erb__790921876_14235864__946051513'
app/controllers/accounts_controller.rb:21:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"y59rGAhS+kqfH3v3axhlYuxvBbBxIWXg0yucCFwfBq8=",
"account"=>{"account_type"=>"dfdf",
"organizations_attributes"=>{"0"=>{"name"=>"dfdf",
"website"=>"dfdf",
"locations_attributes"=>{"0"=>{"phone"=>"dfdf",
"toll_free_phone"=>"dfd",
"fax"=>"",
"addresses_attributes"=>{"0"=>{"address1"=>"",
"address2"=>"",
"city"=>"",
"state"=>"",
"zip"=>""}}}}}}},
"commit"=>"Add account"}
NoMethodError在账户中#创建
显示C:/Documents and Settings/Corey Quillen/My
文档/rails_projects/shop_manager/app/views/accounts/show.html.erb其中第1行
提高:
nil:NilClass的未定义方法“name”
提取的源(第1行附近):
1:
Rails.root:C:/Documents and Settings/Corey Quillen/My
文件/轨道项目/车间经理
应用程序跟踪|框架跟踪|完整跟踪
app/views/accounts/show.html.erb:1:in
`_应用程序视图账户显示html erb 790921876 14235864 94605113'
app/controllers/accounts\u controller.rb:21:in'create'
要求
参数:
{“utf8”=>“utf8”,
“真实性令牌”=>“y59rGAhS+KQFH3V3AXLYUxVBBBXIWXG0YUCCFWFBQ8=”,
“账户”=>{“账户类型”=>“dfdf”,
“组织\u属性”=>{“0”=>{“名称”=>“dfdf”,
“网站”=>“dfdf”,
“位置\u属性”=>{“0”=>{“电话”=>“dfdf”,
“免费电话”=>“dfd”,
“传真”=>“”,
“地址\u属性”=>{“0”=>{“地址1”=>”,
“地址2=>”,
“城市”=>“”,
“状态”=>“”,
“zip”=>“}}”,
“提交”=>“添加帐户”}
由于您的关系是一对多,您必须使用
@organization.first.name
它将显示第一个组织
由于@organization是一个数组,您必须收集所有名称,然后显示。您在显示操作中说@organization=@account.organizations。 您希望@organization包含什么内容? 想想看,它将是一个数组,而不是一个组织,所以循环遍历它并获得每个组织的名称 事实上,我认为你还没有正确地考虑你的关系。一个帐户肯定属于一个组织。你真的希望一个帐户与多个组织关联吗 更新-参考下面的注释 这是完全可能做到的,但您需要确定确定需要在此处显示哪个组织的业务逻辑。 如果你能准确地解释你的关系应该如何运作,那么向你展示如何运用你的逻辑应该不会太困难 更新-如何获取主要组织 这只是在帐户模型上建立一个新关联的问题
has_one :primary_organization.
:class_name => 'Organization',
:conditions => ['primary = ?', true]
然后在你的表演中写下
@account.primary_organization.first #because anything use on a find other than the primary key will always return an array even if there is only one record.
您可能还想检查主组织是否为空?
考虑将HasyOne重构为类方法。根据您的需要,可能不是必需的。
在此处阅读更多内容您是否也可以发布您的“显示”操作的控制器代码?您可能正在“创建”操作中执行
渲染“显示”
,而不是重定向到…
。因此,是的,请向我们显示您的控制器代码。这很有效,但如果我想指定第一个以外的其他组织,该怎么办?我确实想保留帐户和组织之间的一对多关系。是否有方法通过数据库属性指定特定帐户?我的组织表中有一个“primary”布尔列。每个帐户只有一个组织将被标记为“true”。如何通过@account.organizations调用指定此关系?是否有方法可以链接到它?T汉克斯。好的,太好了。听起来像是我在找的。谢谢你的帮助。