我与股票和推荐书有一对一的关系。
在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控制器中添加什么来创建推荐并将其与@share
对象相关联?
我试图将share_id
作为附加参数从视图发送到Testimonials控制器,然后使用“之前”过滤器来查找共享对象,但我不认为这是正确的方法它
答案 0 :(得分: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 %>