接受多个记录的嵌套属性

时间:2013-03-08 03:33:04

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我有两个资源:建议和评级。

建议模型:

class Recommendation < ActiveRecord::Base
  has_many :ratings
  accepts_nested_attributes_for :ratings
end

评级模型:

class Rating < ActiveRecord::Base  
  belongs_to :recommendation
end

建议控制器:

class RecommendationsController < ApplicationController

  def index
    @product = Product.find(params["product_id"])
    @recommendations = Recommendation.find(:all, :joins => :products, :conditions => ["product_id = ?", (params["product_id"])])
    @recommendation = Recommendation.new
    @rating = Rating.new
  end

  def create
    #need to find_or_create new rating based on recommendation_id 
  end  

end

建议指数:

<div id ="prods_container">
  <%= form_for @recommendation do |f| %>
    <%= f.fields_for @rating do |r| %>
      <% @recommendations.each do |rec| %>
        <%= rec.title %>
        <div id="rec_note_text"><%= r.text_field :notes %></div>
      <% end %>
      <%= f.submit %>
    <% end %>
  <% end %>
</div>

我正在尝试为每个“推荐”对象添加备注字段,我需要更新create操作/推荐控制器中的评级表。如何将正确的ID传递给我的控制器以实现此目的?

0 个答案:

没有答案