Ruby on Rails:Create 不起作用。数据不会插入到数据库中



两个简单的模型:

记录
class Record < ActiveRecord::Base
    has_many :comments
    validates :title, presence: true,
                            length: { minimum: 5 }
end

评论
class Comment < ActiveRecord::Base
  belongs_to :record
end
观众

<h2>Add a comment: </h2>
<%= form_for([@record, @record.comments.build]) do |f| %>
    <p>
       <%= f.label :commenter %><br>
    <%= f.text_field :commenter %>
    </p>
    <p>
        <%= f.label :body %><br>
        <%= f.text_area :body %>
    </p>
    <p>
        <%= f.submit %>
    </p>
<% end %>
在控制器

def create
    @record = Record.find(params[:record_id])
    @comment = @record.comments.create(comment_params)
    logger.debug "comment check: #{@comment.attributes.inspect}"
    redirect_to record_path(@record)
end

日志信息:

I, [2014-06-26T14:13:49.802139 #20539]  INFO -- : Processing by CommentsController#create as HTML
I, [2014-06-26T14:13:49.802236 #20539]  INFO -- : Processing by CommentsController#create as HTML
I, [2014-06-26T14:13:49.802300 #20539]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"12u1/KgN8baCy8Jn/TJi3Ux7m258FZPfraHGUhl9WnM=", "comment"=>{"commenter"=>"qwe", "body"=>"asdsa"}, "commit"=>"Create Comment", "record_id"=>"4"}
I, [2014-06-26T14:13:49.802338 #20539]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"12u1/KgN8baCy8Jn/TJi3Ux7m258FZPfraHGUhl9WnM=", "comment"=>{"commenter"=>"qwe", "body"=>"asdsa"}, "commit"=>"Create Comment", "record_id"=>"4"}
D, [2014-06-26T14:13:49.807062 #20539] DEBUG -- :   Record Load (0.1ms)  SELECT  "records".* FROM "records"  WHERE "records"."id" = ? LIMIT 1  [["id", 4]]
D, [2014-06-26T14:13:49.807138 #20539] DEBUG -- :   Record Load (0.1ms)  SELECT  "records".* FROM "records"  WHERE "records"."id" = ? LIMIT 1  [["id", 4]]
D, [2014-06-26T14:13:49.810514 #20539] DEBUG -- :    (0.1ms)  begin transaction
D, [2014-06-26T14:13:49.810599 #20539] DEBUG -- :    (0.1ms)  begin transaction
D, [2014-06-26T14:13:49.821927 #20539] DEBUG -- :    (0.1ms)  commit transaction
D, [2014-06-26T14:13:49.822023 #20539] DEBUG -- :    (0.1ms)  commit transaction
D, [2014-06-26T14:13:49.822159 #20539] DEBUG -- : comment check: {"id"=>nil, "commenter"=>"qwe", "body"=>"asdsa", "record_id"=>nil, "created_at"=>nil, "updated_at"=>nil}
D, [2014-06-26T14:13:49.822201 #20539] DEBUG -- : comment check: {"id"=>nil, "commenter"=>"qwe", "body"=>"asdsa", "record_id"=>nil, "created_at"=>nil, "updated_at"=>nil}
I, [2014-06-26T14:13:49.822667 #20539]  INFO -- : Redirected to http://0.0.0.0:3000/records/4
I, [2014-06-26T14:13:49.822714 #20539]  INFO -- : Redirected to http://0.0.0.0:3000/records/4

当我创建注释时,数据不写入数据库,有什么建议吗?顺便说一下,为什么每个记录器信息有两个输出?

尝试将accepts_nested_attributes_for :comments添加到Record模型

您是否在变量comment_params中捕获了任何内容?难道你不需要做一些类似comment_params = params[:comment]之类的事情吗?我认为comment_params是nil,当你把它放到你的创建方法(虽然我可能是错的)。

def create
  @record = Record.find(params[:record_id])
  @comment = @record.comments.create(params[:comment])
  redirect_to record_path(@record)
end

在控制器中,将comment.create更改为comment.build,以便创建关系。另外,只传递注释的参数:

def create
  @record = Record.find(params[:record_id])
  @comment = @record.comments.build(params[:comment])
  logger.debug "comment check: #{@comment.attributes.inspect}"
  redirect_to record_path(@record) 
end

最新更新