通过关联实现具有多的 Rails 3 嵌套形式

发布于 2024-12-12 05:55:43 字数 3300 浏览 4 评论 0原文

我正在尝试通过关联实现具有多个的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可遇━不可求 2024-12-19 05:55:43

假设你的 MySQL 输出被剪切和粘贴,你可能在迁移中的某个地方出现了拼写错误。 列应为

projec_id

project_fees 表中的

project_id

Assuming your MySQL output is cut and pasted, you probably have a typo in a migration somewhere. The column

projec_id

in your project_fees table should be

project_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文