Search code examples
ruby-on-railsruby-on-rails-5.2

Empty Post in Rails 5.2


I'm trying to add the helper _form of my Comment model inside Advertisement view. I call the helper <%= render 'comments/form', comment: @comment %>in the route /advertisement/:id

So here is my comments _form :

<%= form_with(model: comment, local: true,  url: "/comments") do |form| %>
  <% if comment &&  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="form-group">
    <%= form.label :content %>
    <%= form.text_area :content, class:"form-field" %>
  </div>

  <%= form.hidden_field :advertisment_id, value:params[:id]  %>

  <div class="actions">
    <%= form.submit "Envoyer", class:"btn btn-primary", url: 'comments' %>
  </div>
<% end %>

I specify the url: "/comments" because I'm at /advertisment/:id and by default the action is aimed to here.

But when the post reached my comment controller, it can't read the params.

ActionController::ParameterMissing in CommentsController#create

The strange part is that I've access to the params :

{"utf8"=>"✓", "authenticity_token"=>"0+swOqHPEHN2Gwh0TO3iC7VPRz4ROLoBlkaMkOdnjjYxWHoDer7AwrgnQpu+9VHfSY90yMSRsNp8ojvPJxuzmQ==", "content"=>"Test", "advertisment_id"=>"1", "commit"=>"Envoyer"}

So here is the comment controller :

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

  # [...]

  # POST /comments
  # POST /comments.json
  def create
    @comment = Comment.new(comment_params.merge(:user_id => @session_user.id))

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
        format.json { render :show, status: :created, location: @comment }
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  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(:content, :advertisment_id)
    end
end

Thanks a lot for your help


Solution

  • It probably because your @comment is nil so that form_with works like the old form_tag helper.

    By using form_with, a field like <%= form.text_field :content %>

    • If your model is presented, it will generate <input type="text" name="comment[content]" />
    • If your model is not presented, it will generate <input type="text" name="content" />

    In the second case, your strong params check will reject the naked params

    To be able to solve it, simply assign a valid model to your render

    <%= render 'comments/form', comment: Comment.new %>