开发者

Rails Nested Attribute using Model Data

I am trying to set up a nested model form similar to the one in Ryan Bates' Railscast Ep #196.

My models are:

 class EmployeeReview < ActiveRecord::Base
  belongs_to :employee
  belongs_to :user
  has_many :review_ratings, :dependent => :destroy
  accepts_nested_attributes_for :review_ratings
  attr_accessible :employee_id


class ReviewRating < ActiveRecord::Base
 belongs_to :employee_review
 belongs_to :review_category

class ReviewCategory < ActiveRecord::Base
 has_many :review_ratings

I've already entered in Review Categories, and I have categories separated as a model in case I want to add categories in the future. To display all of the categories in my form, the 'new' action in my EmployeeReviews controller looks like this:

def new
 @title = "Employee Review"
 @review = EmployeeReview.new()
 @employee = Employee.find(params[:employee_id])
 @categories = ReviewCategory.all
  @categories.each do |category|
    @rating = @review.review_ratings.build({:review_category_id => category.id})
  end
end

Then I use the following code for my form:

<h2>Create a review for <%= @employee.first_name %> <%= @employee.last_name %></h2>

<%= form_for @review do |f| %>

<% f.fields_for :review_ratings do |builder| %>
 <b><%= builder.object.review_category.name %></b><br/>
 <%= builder.label :score, "Score" %><br/>
 <%= builder.text_field :score %><br/>
 <%= builder.label :comment, "Comment" %><br/>
 <%= builder.text_area :comment %><br/>
<% end %>

So the form works and displays the correct categories. However, when I submit, it creates the review, but isn't saving the review_ratings associated with it. Here is the code in the 'create' action of my employee_review controller.

de开发者_如何学Pythonf create
 @employee = Employee.find(params[:employee_id])
 @review = EmployeeReview.new(params[:review])
 @review.user_id = current_user.id
 @review.employee_id = @employee.id
 if @review.save
  redirect_to @employee, :flash => {:success => "employee review was created"}
 else
  render 'new', :flash => {:error => "Employee review was not created"}
 end
end

I'm sure it's something obvious, but being very new to rails I can't quite figure it out, even after looking at other nested attribute posts. Any advice is much appreciated.

Update: Here is my schema (the sections for the models)

  create_table "review_categories", :force => true do |t|
   t.text     "description"
   t.datetime "created_at"
   t.datetime "updated_at"
   t.string   "name"
  end

  create_table "review_ratings", :force => true do |t|
   t.integer  "employee_review_id"
   t.integer  "score"
   t.text     "comment"
   t.datetime "created_at"
   t.datetime "updated_at"
   t.integer  "review_category_id"
  end

  create_table "employee_reviews", :force => true do |t|
   t.integer  "employee_id"
   t.datetime "created_at"
   t.datetime "updated_at"
   t.integer  "user_id"
  end


Start with this and see if the fields_for actually shows up. By which I mean you actually have a review when you go to the new action.

def new
 @title = "Employee Review"
 @review = EmployeeReview.new()
 @employee = Employee.find(params[:employee_id])
 @categories = ReviewCategory.all
 conditions = {:review_category_id => ReviewCategory.first.id}
 @rating = @review.review_ratings.build(conditions)
end

First we need to see if fields_for is actually working and then can do you custom stuff. Let me know what happens when you swap out this code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜