Ahmed Elkoussy
Ahmed Elkoussy

Reputation: 8568

Error on submitting form ActionController::ParameterMissing in CommentsController#create param is missing or the value is empty: comment

I am a beginner in Rails and I have been trying to solve this issue for hours. I've searched and applied many solutions but none have worked to solve this issue.

The app is a simple blog with articles and comments. I have made them using scaffold in this project and have them nested in routes.rb with associations in the database.

The problem now is that the partial for the comments form is not working to add any comment at all.

The partial form for the comment:

 <%= 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 %>

Article model:

class Article < ApplicationRecord
    has_many :comments
    accepts_nested_attributes_for :comments
end

Comment model:

class Comment < ApplicationRecord
  belongs_to :article
end

Comments controller:

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

The error I got on the form page for the comment after clicking submit:

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

The server log for that action

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'

Important note:

if I remove params.require(:comment) the submit will pass & I will be redirected to the right page , the issue though is that it won't save the comment itself.

Please advise as I am feeling lost Thank you all.

Upvotes: 1

Views: 399

Answers (2)

Ahmed Elkoussy
Ahmed Elkoussy

Reputation: 8568

Hope this helps someone else , only two changes fixed the code:

1- removing the require commment in the comment_params

    def comment_params
      params**.require(:comment)**.permit(:commenter, :body, :article_id)
    end

2- the create action didn't have a save after making the comment

  def create
    @article = Article.find(params[:article_id])
    @comment = Comment.new(comment_params)
    if @comment.save
      redirect_to article_path(@article)
    end  
  end

Thanks for your help.

Upvotes: 0

PrimeTimeTran
PrimeTimeTran

Reputation: 2137

Your form_with isn't set up correctly. You need to build it so that it is associated with a model.

<%= form_with article_comments_path(Comment.new) do |form| %>
    ... parameters here... 
<% end %>

Will work I believe. Here's a nice article on Medium

EDIT:

This should work if you're in the Articles#Show

def create
  @article = Article.find(params[:id])
  @comment = @article.comments.create(comment_params)
  redirect_to article_path(@article)
end

This will depend on how you've set up routes though.

Upvotes: 1

Related Questions