繁体   English   中英

找不到Rails 4 Param Show Action

[英]Rails 4 Param Not Found Show Action

我正在创建一个博客平台,并且正在努力使我的评论功能起作用。 它正在工作,但突然停止并开始引发此错误。 我的文章显示页面ActionController :: ParameterMissing在CommentsController#create参数中找不到评论表单:评论

这是代码:

comments_controller.rb

class CommentsController < ApplicationController

def create
    @article = Article.find(params[:article_id])
    @comment = @article.comments.create(comment_params)
    flash[:notice] = "Your comment has been saved!"
    redirect_to article_path(@article)
end

def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

    redirect_to(@comment.post)
end

private
def comment_params
    params.require(:comment).permit(:commenter, :comment)
end
end

文章show.html.erb

以下是我的评论表格

<div class="container com_form">
<h4>Add a Comment</h4>
<%= form_for([@article, @article.comments.build]) do |f| %>
  <b><%= f.label :commenter %></b>
  <p><%= f.text_field :commenter %></p>

  <b><%= f.label :comment %></b>
  <p><%= f.text_area :comment %></p>

<p>
<%= f.submit %>
</p>
<% end %>
</div>

应用程序跟踪

app/controllers/comments_controller.rb:19:in `comment_params'
app/controllers/comments_controller.rb:5:in `create

我不确定为什么突然间会出现此错误。 任何帮助是极大的赞赏。

更新

routes.rb

devise_for :users, controllers: {
    sessions: 'users/sessions'
  }

resources :users

resources :articles do
  resources :comments
end

root 'pages#index'
get 'users/sign_in' => 'users/sessions#create'
post 'users/sign_in' => 'users/sessions#create'
post '/articles/:article_id/' => 'comments#create'
post 'articles/new' => 'articles#create'
get 'articles/:id' => 'articles#show'
patch 'articles/:id' => 'articles#update'
get 'atricles/:id/edit' => 'articles#edit'
delete '/articles/:id' => 'articles#destroy'
get 'users/:username' => 'users#show'

链接到错误截图

错误截图

链接到GitHub上的仓库

博客回购

如您所见,发送的参数中没有评论信息。 我不确定,但是尝试在文章的显示视图中将@ article.comments.build更改为Comment.new(author_id:@ article.id)。

在以下行中: <%= form_for([@article, @article.comments.build]) do |f| %> <%= form_for([@article, @article.comments.build]) do |f| %> ,尝试将其更改为Comment.new

您正在此处处理控制器中的Active Record关联:

@comment = @article.comments.create(comment_params)

更新:

更改您的窗体,如下所示:

<%= form_for([@article, Comment.new]) do |f| %>
  <b><%= f.label :commenter %></b>
  <p><%= f.text_field :commenter %></p>

  <b><%= f.label :comment %></b>
  <p><%= f.text_area :comment %></p>

<p>
<%= f.submit %>
</p>

或者,由于要在show controller动作中定义@comment = Comment.new ,因此可以使用:

form_for([@article, @comment])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM