嵌套属性无法保存在数据库中?不显示值-Ruby on Rails [英] Nested attributes not saving in database? Does not display values - Ruby on Rails
问题描述
我有一个嵌套的属性,可以显示值,但是为什么没有将其保存到数据库中?
I have a nested attribute and I can display values, but it is not saving into database why?
我有一个模型review
,我想嵌套属性comments
I have a model review
and I want to nest attribute comments
查看迁移文件
class CreateReviews < ActiveRecord::Migration
def change
create_table :reviews do |t|
t.belongs_to :reviewable, polymorphic: true
t.timestamps
end
add_index :reviews, [:reviewable_id, :reviewable_type]
end
end
评论模型
class Review < ActiveRecord::Base
attr_accessible :rating, :user_id, :comments_attributes, :service
has_many :comments, as: :commentable
belongs_to :user
belongs_to :reviewable, polymorphic: true
accepts_nested_attributes_for :comments
end
评论迁移文件
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :content
t.belongs_to :commentable, polymorphic: true
t.integer :user_id
t.timestamps
end
add_index :comments, [:commentable_id, :commentable_type]
end
end
评论模型
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :commentable, polymorphic: true
belongs_to :user
belongs_to :review
end
这是表格
<%= form_for [@reviewable, @review], :html => {:multipart => true} do |f| %>
<%= f.fields_for :comments, @review.comments.build do |field| %>
<%= field.label "Comment" %></div>
<%= field.text_field :content %>
<% end %>
<%= f.submit %>
<% end %>
它显示字段,但是保存时为什么不保存呢?
It displays the fields, but how come when I save, it doesn't save?
我试图这样显示:
<%= @review.comments %>
这是我的评论控制者
def new
@review = @reviewable.reviews.new
@review.comments.build
end
感谢您的帮助!
查看控制器:创建方法
def create
@review = @reviewable.reviews.new(params[:review])
@review.user = current_user
if @review.save
redirect_to @reviewable, notice: "review created."
else
render :new
end
end
推荐答案
您的控制器和视图中似乎存在一些混乱的逻辑
There seems to be some muddled logic in your controller and views
def new
@review = @reviewable.reviews.new
@review.comments.build
end
在这里您正在呼叫@reviewable.reviews.new
,但是@reviewable
尚不存在.一种更常见的调用方式是Review.new
Here you are calling @reviewable.reviews.new
, but @reviewable
doesn't yet exist. A more usual way of calling this would be Review.new
接下来,您要执行此操作
Next in your view you do this
<%= form_for [@reviewable, @review], :html => {:multipart => true} do |f| %>
<%= f.fields_for :comments, @review.comments.build do |field| %>
这会将@reviewable
和@review
传递给form_for
.这似乎很奇怪.使用嵌套,您通常只需要传递顶级实例变量,即@review
.
This is passing @reviewable
and @review
to the form_for
. This seems odd. With nesting you would normally only need to pass the top level instance variable i.e. @review
.
接下来,当您进入fields_for
时,您将传递:comments
和@review.comments.build
.一般来说,这只需要:comment
,因为您已经在控制器中建立了注释.
Next, when you get to fields_for
you are passing :comments
and @review.comments.build
. Generally speaking this should only require :comment
, because you've already built the comment in the controller.
我建议您查看这两个截屏视频
I would recommend that you review these two screencasts
http://railscasts.com/episodes/196-nested- model-form-part-1 http://railscasts.com/episodes/197-nested-model-form -part-2
还有许多宝石可以处理使事物变得雄辩可言的整个过程,可以在这里找到
There are also a number of Gems that handle the whole process of making things commentable very eloquently, they can be found here
https://www.ruby-toolbox.com/categories/rails_comments
我认为值得评论的宝石之路更好,因为它们正在由更大的社区进行研究,从长远来看,我认为最终要比个人创造更好.
I think the commentable Gems path is better because they are being worked on by a larger community and I think end up being better than personal creations in the long term.
希望这对您有帮助...
Hope this helps...
这篇关于嵌套属性无法保存在数据库中?不显示值-Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!