0

我与 Shares 和 Testimonials 是一对一的关系。

在 Testimonial.rb 中:

belongs_to :share

在 Share.rb 中:

has_one :testimonial

我想向“共享”页面添加一个表单,我可以在其中创建属于该特定共享的推荐。

在我设置的 SharesController 内部:

@testimonial = @share.build_testimonial

在“共享”视图中,我有:

<%= form_for @testimonial do |f| %>  
 <%= f.text_area :message %>
<%= f.submit "Submit testimonial" %>

以上是正确的还是我必须以某种方式将共享对象添加到视图中?

我应该向 Testimonials 控制器中的 create 操作添加什么来创建推荐并将其与@share对象相关联?

我尝试将share_id视图作为附加参数发送到 Testimonials 控制器,然后使用“之前”过滤器来查找共享对象,但我认为这不是正确的方法。

4

1 回答 1

0

share.rb模型中,您需要添加:

accepts_nested_attributes_for :testimonial

既然您在share视图中,我认为您在该视图中会有一个构造:

<%= form_for @share do |f| %>
  ...
<% end %>

在这种情况下,要使用这些testimonial字段:

<%= form_for @share do |f| %>
  <%= fields_for @share.testimonial do |t| %>
    <%= t.text_area :message %>
  <% end %>
<% end %>
于 2013-09-25T11:35:03.810 回答