Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 无法将信息保存到数据库列_Ruby On Rails_Ruby - Fatal编程技术网

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

Ruby on rails 无法将信息保存到数据库列,ruby-on-rails,ruby,Ruby On Rails,Ruby,我使用Ruby(RubyonRails)添加了一个照片发布功能,并实现了它,这样您就可以在细节屏幕(showAction)上发布评论。 但是,当您写评论并单击“发布”按钮时,屏幕不会发生任何更改,您试图在列中发布的字符也不会保存 注释控制器 class CatPostCommentsController < ApplicationController def create @cat_post_comment = CatPostComment.create(cat_commen

我使用Ruby(RubyonRails)添加了一个照片发布功能,并实现了它,这样您就可以在细节屏幕(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
但是我没有得到错误的来源,所以随机更改了渲染路径 但是没有起作用。 然后尝试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>)> 
3:def创建
4:@cat_post_comment=CatPostComment.create(cat_comment_参数)
=>5:binding.pry
6:if@cat\u post\u comment.save
7:呈现“/cat_main/#{cat_post_comment.cat_post.id}”
8:完
9:完
[1] 窥探(#)>@cat_post_评论
=> #
[2] pry(#)>@cat_post_comment.save!
ActiveRecord::RecordInvalid:
验证失败:请输入Cat post。
来自/Users/ayumiuchimura/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.3.2/lib/active\u record/validations.rb:80:in“raise\u validation\u error”
[3] pry(#)>@cat_post_comment.errors.full_消息
=>[“请输入Cat post。”]
[4] 窥探(#)>@cat_post_评论
=> #
[5] 撬动(#)>
原因是数据库无法获取cat_post_id,终端称其为nil。 我试图更改id的描述,比如“params:cat_post_ids=>[]”,或者将其置于允许状态,但没有任何效果。 如果有人知道如何修复它,那就太好了。
我期待得到一些回复,谢谢。

我认为应该是
render”/cat\u main/{cat\u post.cat\u post\u comment.id}“
,因为您的cat\u post\u评论属于cat\u post,而不是相反

编辑

再次查看您的代码,我认为它应该是
render”/cat_main/{cat_post_comment.id}

我认为它应该是
render”/cat_main/{cat_post.cat_post_comment.id}
,因为您的cat_post_comment属于cat_post,而不是其他方式

编辑

再次查看您的代码,我认为它应该是
render”/cat_main/{cat_post_comment.id}
请求中的参数没有
cat_post_id
,而是有
cat_main_id
,因此您无法保存
cat_post_comment
,因为它缺少
cat_post_id
,因此,请检查
cat_main_id
是否可以替换
cat_post_id
您在视图中的检查表单请求中的参数没有
cat_post_id
,但它有
cat_main_id
,因此您无法保存
cat_post_注释
,因为它缺少
cat_post_id
,因此,请检查
cat\u main\u id
是否可以替换
cat\u post\u id
查看您的表单

谢谢您的回复!呈现路径有问题,正如您所说,我更改为{@cat_post_comment.cat_post.id},然后将注释保存到列!!非常感谢!真的很有帮助!!谢谢你的回复!呈现路径有问题,正如您所说,我更改为{@cat_post_comment.cat_post.id},然后将注释保存到列!!非常感谢!真的很有帮助!!谢谢你的回复!我甚至没有注意到id是错误的,并检查了控制器名称和其他文件。然后,将名称更改为cat_post并修复了它!非常感谢你!!这真的很有帮助!!谢谢你的回复!我甚至没有注意到id是错误的,并检查了控制器名称和其他文件。然后,将名称更改为cat_post并修复了它!非常感谢你!!这真的很有帮助!!
  <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)

    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>)>