无法将信息保存到数据库列

时间:2020-08-21 00:46:58

标签: ruby-on-rails ruby

我正在使用Ruby(Ruby on Rails)添加照片发布功能并实现它,以便您可以在详细信息屏幕(showAction)中发布评论。 但是当您写评论并单击“发布”按钮时,屏幕没有变化,并且您尝试在该列中发布的字符也不会保存。

评论控制器

class CatPostCommentsController < ApplicationController

  def create
    @cat_post_comment = CatPostComment.create(cat_comment_params)
    if @cat_post_comment.save
      render "/cat_main/#{cat_post_comment.cat_post.id}"
    end
  end

  private
  def cat_comment_params
    params.require(:cat_post).permit(:cat_post_comment).merge(user_id: current_user.id, cat_post_id: params[:cat_post_id])
  end
end

评论模型

class CatPostComment < ApplicationRecord
  belongs_to :user
  belongs_to :cat_post

  validates :cat_post_comment, presence: true
end

查看文件(显示操作)

  <div class="container">
      <%= form_with(model: [@cat_post_comment,@cat_post], url:cat_main_cat_post_comments_path(@cat_post), method: :post, local: true) do |form| %>
        <%= form.text_area :cat_post_comment, placeholder: "add your comments" %>
       <div class="comment_btn"><%= form.submit "subimit" %></div>
      <% end %>
  </div>

浏览器上没有错误消息,并检查了终端,并在下面发现了错误。

Started POST "/cat_main/5/cat_post_comments" for ::1 at 2020-08-20 22:35:20 +0900
Processing by CatPostCommentsController#create as HTML
  Parameters: {"authenticity_token"=>"DgF8MsAPQAqJQzw9GtUoRg5EL+3xWnd9+QZbmGDpo44v4UyauD16C/mKdXhdwP/bSAMmt30ncBV7+/GlDbxlPg==", "cat_post"=>{"cat_post_comment"=>"test\r\n"}, "commit"=>"submit", "cat_main_id"=>"5"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
   (0.2ms)  BEGIN
  ↳ app/controllers/cat_post_comments_controller.rb:4:in `create'
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  ↳ app/controllers/cat_post_comments_controller.rb:4:in `create'
   (0.2ms)  ROLLBACK
  ↳ app/controllers/cat_post_comments_controller.rb:4:in `create'
No template found for CatPostCommentsController#create, rendering head :no_content
Completed 204 No Content in 10ms (ActiveRecord: 0.9ms | Allocations: 7721)

但是我没有得到错误的根源,所以随机更改了渲染路径……但是没有用。 然后尝试binding.pry找出问题所在

    3: def create
    4:   @cat_post_comment = CatPostComment.create(cat_comment_params)
 => 5:   binding.pry
    6:   if @cat_post_comment.save
    7:     render "/cat_main/#{cat_post_comment.cat_post.id}"
    8:   end
    9: end

[1] pry(#<CatPostCommentsController>)> @cat_post_comment
=> #<CatPostComment:0x00007fc6ba1fe540 id: nil, user_id: 1, cat_post_id: nil, cat_post_comment: "テスト", created_at: nil, updated_at: nil>
[2] pry(#<CatPostCommentsController>)> @cat_post_comment.save!
ActiveRecord::RecordInvalid: 
Validation failed: please enter Cat post.
from /Users/ayumiuchimura/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.3.2/lib/active_record/validations.rb:80:in `raise_validation_error'
[3] pry(#<CatPostCommentsController>)> @cat_post_comment.errors.full_messages
=> ["please enter Cat post."]
[4] pry(#<CatPostCommentsController>)> @cat_post_comment
=> #<CatPostComment:0x00007fc6ba1fe540 id: nil, user_id: 1, cat_post_id: nil, cat_post_comment: "テスト", created_at: nil, updated_at: nil>
[5] pry(#<CatPostCommentsController>)> 

原因是数据库无法获取cat_post_id,并且终端说它为nil。 我试图更改ID的描述,例如“ params:cat_post_ids => []”,或允许它使用,但没有任何效果。 如果有人知道如何修复它,那就太好了。 我期待收到一些答复,谢谢。

2 个答案:

答案 0 :(得分:2)

您的请求中的参数没有cat_post_id,但有cat_main_id,因此您无法保存cat_post_comment,因为它缺少cat_post_id,因此请检查{ {1}}可以代替cat_main_id,以查看您的表单

答案 1 :(得分:1)

我认为应该是render "/cat_main/#{cat_post.cat_post_comment.id}",因为您的cat_post_comment属于cat_post,而不是反过来。

修改
再次查看您的代码,我认为应该只是render "/cat_main/#{cat_post_comment.id}"

相关问题