Ruby on rails 指定一个模型';通过关联来定义属性
我是rails新手,正在开发rails应用程序,我一直在思考这个问题 我有三种型号Ruby on rails 指定一个模型';通过关联来定义属性,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我是rails新手,正在开发rails应用程序,我一直在思考这个问题 我有三种型号 class Product < ActiveRecord::Base attr_accessible :name, :issn, :category, :user_products_attributes validates_presence_of :name, :issn, :category validates_numericality_of :issn, :message =
class Product < ActiveRecord::Base
attr_accessible :name, :issn, :category, :user_products_attributes
validates_presence_of :name, :issn, :category
validates_numericality_of :issn, :message => "has to be a number"
has_many :user_products
has_many :users, :through => :user_products
accepts_nested_attributes_for :user_products
end
class UserProduct < ActiveRecord::Base
attr_accessible :price, :category
validates_presence_of :price, :category
validates_numericality_of :price, :message => "has to be a number"
belongs_to :user
belongs_to :product
end
class user < ActiveRecord::Base
# devise authentication here
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :user_products, :dependent => :destroy
has_many :products, :through => :user_products
end
结束
所以问题是:我希望用户在表单中输入产品的信息,但这也涉及到输入与产品关联的不同模型/表格(用户产品)中的产品价格。我该怎么做?您可以看到我的表单_使用@product
任何帮助都将不胜感激
<div class="span8">
<div id="listBoxWrapper">
<fieldset>
<%= form_for(@product, :html => { :class => "form-inline" }, :style => "margin-bottom: 60px" ) do |f| %>
<div class="control-group">
<label class="control-label" for="Category">Category</label>
<div class="controls">
<%= f.text_field :category, :class => 'input-xlarge', :id => "Category" %>
</div>
</div>
<div class="control-group" style="display: inline-table;">
<label class="control-label" for="First Name">Price($/Month)</label>
<div class="controls">
<%= product.fields_for :user_products do |p| %>
<%= p.text_field :price, :class => 'input-xlarge input-name' %>
<% end %>
</div>
</div>
{:class=>“forminline”},:style=>“marginbottom:60px”)do | f |%>
类别
'输入xlarge',:id=>“类别”%>
价格(美元/月)
'输入xlarge输入名称'%>
应该是以下类型
<%= form_for(@product) do |product| %>
<%= product.text_field :name, :class => 'input-xlarge input-name' %>
<%= product.fields_for :user_products do |user_product| %>
<%= user_product.text_field :price, :class => 'input-xlarge input-name' %>
accepts_nested_attributes_for :user_products
您的产品
型号应具有以下特性
<%= form_for(@product) do |product| %>
<%= product.text_field :name, :class => 'input-xlarge input-name' %>
<%= product.fields_for :user_products do |user_product| %>
<%= user_product.text_field :price, :class => 'input-xlarge input-name' %>
accepts_nested_attributes_for :user_products
这将使人们意识到,user\u products
值可能在保存产品实体时出现
编辑:
只是解释嵌套形式的骨架
form_for(@object) do |object_form_builder|
# call any field generator helper function by object_form_builder like
object_form_builder.text_field
object_form_builder.check_box
# so on...
#Now for nested forms get the nested objects from the builder like
object_form_builder.fields_for :nested_objects do |nested_object_builder|
#generate the fields for this with nested_object_builder. Like
nested_object_builder.text_field
# so on...
end
end
是的,为对象生成器使用短名称总是很方便的,就像您通常为
form\u builder使用f
一样。好的,我正在使用您的解决方案。但是我得到一个错误,说product.fields_for中的product是一个未定义的局部变量或方法…使用build的目的是什么?谢谢你粘贴你的表格
代码。因为如果product.text\u字段
可以运行,product.fields\u没有理由失败build
是一种告诉@product
我可能还需要为这些实体生成字段的机制。当您只想生成@product
的形式时,您可以通过product.new
构建产品。但是这里还需要@product.user\u products
的字段。所以你必须建立它,我修正了错误。页面已加载,但价格的文本字段未显示。我会尝试使用product
和user\u-product
(或类似于模型实例的内容)在表单中保留块变量,例如f
,或一些简单的变量。在他发布的更新中,你可以看到他使用了product.fields\u作为f.fields\u作为。在安装完产品后,您可以在控制器中执行此操作,@Product.user\u products.build
我遵循了您的步骤和railscast教程。我已经编辑了上面的代码…现在我得到一个“未知属性:book\u id”错误…我似乎认为我的用户产品的命名约定是错误的…谢谢!对不起,我是说产品id…我想我现在可以用了…我刚刚在我的用户产品表中添加了一个产品id…一切正常…非常感谢!!。。。。然而,我正在重新思考我建立所有模型的方式……可能不是最好的方式……如果你在意,我可以解释,也许你可以提供更好的方式。再次感谢你为什么不。。。让我们看看:)好的,酷…我会打一些东西,大约一个小时后发送…我可能需要开始另一个讨论。再次感谢