title: How to add comments on a view?
body: <p>I'm learning Rails and I try to add a comment function.</p>
  
  <p>I have this error when I try to add a comment:
  <strong>No route matches [POST] "/advertisements/1"</strong></p>
  
  <p>How can I say to Rails : "Hey dude, I would like you go to my comments#create function"</p>
  
  <pre><code>#app/views/advertisements/show.html.erb
  &lt;% if @current_user %&gt;
      &lt;%= render "comments/form", comment: @comment %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <pre><code>#app/views/comments/_form.html.erb
  &lt;%= form_with(model: comment, local: true) do |form| %&gt;
  
    &lt;div class="field"&gt;
      &lt;%= form.text_area :content %&gt;
    &lt;/div&gt;
  
    &lt;div class="field"&gt;
      &lt;%= form.hidden_field :user_id, :value =&gt; @current_user.id %&gt;
    &lt;/div&gt;
  
    &lt;div class="field"&gt;
      &lt;%= form.hidden_field :advertisement_id, :value =&gt; @advertisement.id %&gt;
    &lt;/div&gt;
  
    &lt;div class="actions"&gt;
      &lt;%= form.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <pre><code>#app/controllers/comments_controller.rb
  class CommentsController &lt; ApplicationController
  
    # GET /comments/new
    def new
      @comment = Comment.new
    end
  
    # POST /comments
    # POST /comments.json
    def create
      @comment = Comment.new(comment_params)
  
      respond_to do |format|
        if @comment.save
          format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
        else
          format.html { render :new }
        end
      end
    end
  
    private
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def comment_params
        params.require(:comment).permit(:content, :user_id, :advertisement_id)
      end
  end
  
  </code></pre>
  
  <p>HTML form rendered (why he has "/advtertisements/1" in action input</p>
  
  <pre><code>&lt;form action="/advertisements/1" accept-charset="UTF-8" method="post"&gt;
  
  &lt;input type="hidden" name="authenticity_token" value="pJOwu9LyhQmj4uSYhyeBKD66Zab+kJf9bR1m+eJJ1Fj+kznMl/4rVz69ucPa2gkuOc/yUB8rPLWhpH+6GHWaqQ==" /&gt;
  
    &lt;div class="field"&gt;
      &lt;textarea name="content" id="content"&gt;
  &lt;/textarea&gt;
    &lt;/div&gt;
  
    &lt;div class="field"&gt;
      &lt;input value="1" type="hidden" name="user_id" id="user_id" /&gt;
    &lt;/div&gt;
  
    &lt;div class="field"&gt;
      &lt;input value="1" type="hidden" name="advertisement_id" id="advertisement_id" /&gt;
    &lt;/div&gt;
  
    &lt;div class="actions"&gt;
      &lt;input type="submit" name="commit" value="Save " data-disable-with="Save " /&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <pre><code>#config/routes.rb
  Rails.application.routes.draw do
  
    resources :comments, only: [:new, :create]
    resources :advertisements
    resources :users, only: [:new, :create] do
      collection do
        get :login, to: 'sessions#new', as: "login"
        post :login, to: 'sessions#create'
        delete :logout, to: 'sessions#destroy', as: "logout"
      end
    end
    # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  end
  </code></pre>
  
  <pre><code>#app/controllers/advertisements_controller.rb
  before_action :set_advertisement, only: [:show, :edit, :update, :destroy]
  
  ...
  
  # GET /advertisements/1
  # GET /advertisements/1.json
  def show
    @comments = @advertisement.comments.all
  end
  
  ...
  
  private
  # Use callbacks to share common setup or constraints between actions.
  def set_advertisement
    @advertisement = Advertisement.find(params[:id])
  end
  </code></pre>
  
  <p>I know hidden_fields and form like mine are probably not the best option to do this kind of thing btw but I'm still learning. Thank you for your help ! </p>