通过关联实现具有多的 Rails 3 嵌套形式
我正在尝试通过关联实现具有多个的 Rails3 嵌套表单。
我的模型关系如下(我的模型是 Project ProjectDuration、ProjectFee)。通过project_fees,项目可以有多个项目持续时间。
以下是我的模型/表
mysql> desc projects;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
class Project < ActiveRecord::Base
has_many :project_durations, :through => :project_fees
has_many :project_fees
accepts_nested_attributes_for :project_fees
end
mysql> desc project_durations;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| duration | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
class ProjectDuration < ActiveRecord::Base
has_many :projects, :through => :project_fees
has_many :project_fees
end
mysql> desc project_fees;
+---------------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| projec_id | int(11) | YES | | NULL | |
| project_duration_id | int(11) | YES | | NULL | |
| fee | float | YES | | NULL | |
+---------------------+----------+------+-----+---------+----------------+
class ProjectFee < ActiveRecord::Base
belongs_to :projects
belongs_to :project_durations
end
我的projects_controllers新操作如下
class ProjectsController < AdminsController
def new
@project = Project.new
@project_durations = ProjectDuration.find(:all)
project_fees = @project.project_fees.build()
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @project }
end
end
end
我终于有了以下视图(new.erb),
<%= form_for(@project, :html => { :class => :form }) do |f| -%>
<% @project_durations.each do |duration| %>
<%= f.fields_for :project_fees do |builder| %>
<%= render 'fee_fields', :f => builder, :project => @project, :duration => duration %>
<% end %>
<% end %>
<% end -%>
并且'fee_fields'是这样
<ul>
<li>
<%= duration.duration %>
<%= f.text_field :fee %>
<%= f.hidden_field :project_duration_id, :value => duration.id %>
</li>
</ul>
即使这将数据保存到'project_fees'表,它不会将数据保存在 project_fees
表的 project_id
字段中。
我在 Linux 上使用 Rails 3 和 Ruby 1.8.7。
I'm trying to implement a Rails3 nested form with has-many through association.
My model relationships are as follows (My models are Project ProjectDuration, ProjectFee). Project can have many project duration through project_fees.
Following are my models/table
mysql> desc projects;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
class Project < ActiveRecord::Base
has_many :project_durations, :through => :project_fees
has_many :project_fees
accepts_nested_attributes_for :project_fees
end
mysql> desc project_durations;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| duration | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
class ProjectDuration < ActiveRecord::Base
has_many :projects, :through => :project_fees
has_many :project_fees
end
mysql> desc project_fees;
+---------------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| projec_id | int(11) | YES | | NULL | |
| project_duration_id | int(11) | YES | | NULL | |
| fee | float | YES | | NULL | |
+---------------------+----------+------+-----+---------+----------------+
class ProjectFee < ActiveRecord::Base
belongs_to :projects
belongs_to :project_durations
end
And my projects_controllers new action is as follows
class ProjectsController < AdminsController
def new
@project = Project.new
@project_durations = ProjectDuration.find(:all)
project_fees = @project.project_fees.build()
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @project }
end
end
end
And I finally I have the following view (new.erb)
<%= form_for(@project, :html => { :class => :form }) do |f| -%>
<% @project_durations.each do |duration| %>
<%= f.fields_for :project_fees do |builder| %>
<%= render 'fee_fields', :f => builder, :project => @project, :duration => duration %>
<% end %>
<% end %>
<% end -%>
and 'fee_fields' is as
<ul>
<li>
<%= duration.duration %>
<%= f.text_field :fee %>
<%= f.hidden_field :project_duration_id, :value => duration.id %>
</li>
</ul>
Even though this saves data to 'project_fees' table, it does not save data in the project_id
field of the project_fees
table.
I'm using Rails 3 with Ruby 1.8.7 on Linux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设你的 MySQL 输出被剪切和粘贴,你可能在迁移中的某个地方出现了拼写错误。 列应为
project_fees
表中的Assuming your MySQL output is cut and pasted, you probably have a typo in a migration somewhere. The column
in your
project_fees
table should be这对我来说是救星
http ://iqbalfarabi.net/2011/01/20/rails-nested-form-with-has-many-through-association/
This was the life saver for me
http://iqbalfarabi.net/2011/01/20/rails-nested-form-with-has-many-through-association/