使用模型数据的 Rails 嵌套属性
我正在尝试建立一个类似于 Ryan Bates 的 Railscast Ep #196 中的嵌套模型表单。
我的模型是:
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
我已经输入了“评论类别”,并且我将类别作为模型分开,以防我将来想添加类别。为了显示表单中的所有类别,我的 EmployeeReviews 控制器中的“新”操作如下所示:
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
然后我对表单使用以下代码:
<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 %>
这样表单就可以工作并显示正确的类别。但是,当我提交时,它会创建评论,但不会保存与其关联的 review_ ratings 。这是我的employee_review 控制器的“创建”操作中的代码。
def 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
我确信这是显而易见的事情,但是对于 Rails 来说,我还是不太明白,即使在查看了其他嵌套属性帖子之后也是如此。非常感谢任何建议。
更新:这是我的架构(模型的部分)
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
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.
def 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从这里开始,看看
fields_for
是否实际显示。我的意思是,当您进行新操作时,您实际上会进行审查。首先,我们需要查看
fields_for
是否实际工作,然后可以执行自定义操作。让我知道当您更换此代码时会发生什么。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.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.