1

server log:

`ActionController::ParameterMissing (param is missing or the value is empty: comment)`

used pry gem gem params:

 <ActionController::Parameters {"utf8"=>"✓", "comment_name"=>"123432",
 "comment_description"=>"231424", "commit"=>"submit", 
"controller"=>"comments", "action"=>"create", "article_id"=>"5"} permitted: false>

I know the :comment should wrapper coment_name and comment_description

so on validate add submitHandler try fix the format error

click submit button the browser show:

enter image description here

jquery validate:

$(function () {

    $("form#new_comment").validate({
        rules: {
            comment_name: {
                required: true,
                minlength: 3
            },

            comment_description: {
                required: true,
                minlength: 5
            },
            submitHandler: function (form) {
                $.ajax({
                    url: form.action,
                    type: form.method,
                    data: $(form).serializeArray(),
                    dataType: 'script'
                });
            }
        }
    });
});

_form.html.erb:

<%= simple_form_for [@article, @article.comments.build], remote: true do |f| %>
    <%= f.input :name, input_html: { name: "comment_name"} %>
    <%= f.input :description, input_html: { name: "comment_description" } %>
    <%= f.submit :submit, class: "btn btn-default" %>
<% end %>

comment_controller:

class CommentsController < ApplicationController

  before_action :get_article
  before_action :authenticate_user!

  def create
    @comment = @article.comments.create(comment_params)
    @comment.user_id = current_user.id
    if @comment.save
      respond_to do |format|
        format.html { redirect_to @article, notice: "creaed succeed"}
        format.js { }
      end
    else
      redirect_to @article, notice: "characters is too short or name has been taken"
    end
  end

  def destroy
    @comment = @article.comments.find(params[:id])
    if @comment.destroy
      respond_to do |format|
        format.html { redirect_to @article }
        format.js { }
      end
    end
  end

  private

  def comment_params
    params.require(:comment).permit(:name, :description, :article_id, :user_id)
  end

  def get_article
    @article = Article.find(params[:article_id])
  end
end

any help thank 🙃

1 Answer 1

1

The controller expects

<ActionController::Parameters {"utf8"=>"✓", "comment"=>
{name"=>"123432","description"=>"231424"}, "commit"=>"submit", 
"controller"=>"comments", "action"=>"create", "article_id"=>"5"} 
permitted: false>

In your form, by declaring "name" attributes for the 'name' and 'description' fields, you are essentially overwriting the "name" field's name from comment[name] to comment_name. So just remove those name attributes from your form

<%= simple_form_for [@article, @article.comments.build], remote: true do |f| %>
 <%= f.input :name%>
 <%= f.input :description%>
 <%= f.submit :submit, class: "btn btn-default" %>
<% end %>`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.