我是Rails的初学者,我一直试图解决这个问题好几个小时。我已经搜索并应用了许多解决方案,但没有人能够解决这个问题。
该应用是一个简短的博客,包含文章和评论。我在这个项目中使用scaffold创建了它们,并将它们嵌套在routes.rb
中,并在数据库中包含关联。
现在的问题是,评论表单的部分内容根本无法添加任何评论。
评论的部分表格:
<%= form_with(url: article_comments_path) do |form| %>
<% if comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% comment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :commenter %>
<%= form.text_field :commenter, id: :comment_commenter %>
</div>
<div class="field">
<%= form.label :body %>
<%= form.text_field :body, id: :comment_body %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
文章模型:
class Article < ApplicationRecord
has_many :comments
accepts_nested_attributes_for :comments
end
评论模型:
class Comment < ApplicationRecord
belongs_to :article
end
评论控制员:
def create
@article = Article.find(params[:article_id])
@comment = Comment.new(comment_params)
redirect_to article_path(@article)
#article_id = params[:article_id]
#raise params.inspect
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:commenter, :body)
end
点击提交后,我在评论的表单页面上收到错误:
ActionController::ParameterMissing in CommentsController#create
param is missing or the value is empty: comment Extracted source (around line #437):
#435 value
#436 else
*437 raise ParameterMissing.new(key)
#438 end
#439 end
#440
Extracted source (around line #73):
#71 # Never trust parameters from the scary internet, only allow the white list through.
#72 def comment_params
*73 params.require(:comment).permit(:commenter, :body)
#74 end
#75 end
Extracted source (around line #36):
#34
#35 @article = Article.find(params[:article_id])
*36 @comment = @article.comments.create(comment_params)
#37 redirect_to article_path(@article)
#38
#39 end
的routes.rb
Prefix Verb URI Pattern Controller#Action
rails_admin /admin RailsAdmin::Engine
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PATCH /articles/:article_id/comments/:id(.:format) comments#update
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
该操作的服务器日志
Started POST "/articles/8/comments" for 127.0.0.1 at 2017-09-02 20:18:44 +0300
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"vy35XCR5S4E7rREcavLifxAPqwzPOrl0bVbbWA4nyXgQDWFLxJYu/2YVa/NdnJGBwN7mCW8RjliPUnkGPtPwnQ==", "commenter"=>"asdasd", "body"=>"sdfdfgdf", "commit"=>"Save ", "article_id"=>"8"}
[1m[36mArticle Load (5.2ms)[0m [1m[34mSELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2[0m [["id", 8], ["LIMIT", 1]]
Completed 400 Bad Request in 35ms (ActiveRecord: 5.2ms)
ActionController::ParameterMissing (param is missing or the value is empty: comment):
app/controllers/comments_controller.rb:71:in `comment_params'
app/controllers/comments_controller.rb:33:in `create'
重要提示:
如果我删除
params.require(:comment)
提交将通过&amp;我将被重定向到正确的页面,但问题是它不会保存评论本身。
请告知我感到迷茫 谢谢大家。
答案 0 :(得分:1)
您的form_with
设置不正确。您需要构建它以使其与模型相关联。
<%= form_with article_comments_path(Comment.new) do |form| %>
... parameters here...
<% end %>
我相信会工作吗? 这是关于Medium
的好文章编辑:
如果你在Articles#Show
def create
@article = Article.find(params[:id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end
这取决于你如何设置路线。
答案 1 :(得分:0)
希望这可以帮助其他人,只有两项更改修复了代码:
1-删除comment_params
中的require commment def comment_params
params**.require(:comment)**.permit(:commenter, :body, :article_id)
end
2-创建操作在发表评论后没有保存
def create
@article = Article.find(params[:article_id])
@comment = Comment.new(comment_params)
if @comment.save
redirect_to article_path(@article)
end
end
感谢您的帮助。