Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 动态添加行(每行都是关联的对象实例)_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 动态添加行(每行都是关联的对象实例)

Ruby on rails 动态添加行(每行都是关联的对象实例),ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有一个模型级“市场”,它有许多产品: class Market < ActiveRecord::Base has_many :products end 如果在新建/编辑市场视图中使用此haml文件,如何实现?您需要的是一个嵌套的属性。这本书读起来有点长,但这里有一个指向railscast的链接: 这是rails 3的更新railscast: 这分为两部分:) 如果您想立即深入研究代码:是的,我已经在rails 3中尝试过了。我更新了我的答案,找到了一个更合适的链接(对于rails

我有一个模型级“市场”,它有许多产品:

class Market < ActiveRecord::Base
  has_many :products
end

如果在新建/编辑市场视图中使用此haml文件,如何实现?

您需要的是一个嵌套的属性。这本书读起来有点长,但这里有一个指向railscast的链接:

这是rails 3的更新railscast:

这分为两部分:)


如果您想立即深入研究代码:

是的,我已经在rails 3中尝试过了。我更新了我的答案,找到了一个更合适的链接(对于rails 3)伟大的链接。谢谢你,我要试试;)
class Product < ActiveRecord::Base
  belongs_to :market
end
<div>
    <input type=text name="name" size=10 value="Name">
    <input type=text name="price" size=10 value="Price">
    <input type=text name="category" size=10 value="category">
<div>

<div>
   <input type=text name=z3 size=10>
   <input type=text name=z3 size=10>
   <input type=text name=z3 size=10>
<div>
...When "Add product" button pressed, a new row of product input fields (div block) is added
<br>
<input type="submit" name="Add" value="Add product">
=form_for :market do |form|
 ...
  =fields_for "product" market.product do |field|
    =fields.text_field :name
    =fields.text_field :price
    =fields.text_field :category
   /...When "Add product" button pressed, a new row of product input fields is added
  =field.submit "Add product"
form.submit "Save"