Home > other >  No route matches [POST] when using a form_with with a post request
No route matches [POST] when using a form_with with a post request

Time:06-19

I'm trying to send a simple information to my controller using a form_with, but the application doesn't identify my post type route even though it's created.

#routes.rb

Rails.application.routes.draw do
 https://guides.rubyonrails.org/routing.html


  get 'report_client', to: 'report_client#index'
  post 'report_client', to: 'report_client#create'

  root "home#index"
end
#view

<%= form_with url: "report_client_path", method: :post do |f| %>
  <%= f.label :salary, "Salary: " %><br><br>
  <%= f.text_field :salary %>
  <button type="submit">Enviar</button>
<% end %>
#controller

class ReportClientController < ApplicationController

  def index
  end

  def create
  end
end
#rails routes

Path / Url          HTTP Verb    Path Match                 Controller#Action

report_client_path  GET          /report_client(.:format)   report_client#index                                                                        
                    POST         /report_client(.:format)   report_client#create
root_path           GET          /                          home#index

CodePudding user response:

Replace "report_client_path" to report_client_path. Just remove double quotes for report_client_path helper.

<%= form_with url: report_client_path, method: :post do |f| %>
  <%= f.label :salary, "Salary: " %><br><br>
  <%= f.text_field :salary %>
  <button type="submit">Enviar</button>
<% end %>

CodePudding user response:

You're passing the string literal "report_client_path" instead of calling the method report_client_path which means that the form will have action="/report_client_path" instead of report_client. Remove the quotes or use the correct string literal.

<%= form_with url: report_client, method: :post do |f| %>
  <%= f.label :salary, "Salary: " %><br><br>
  <%= f.text_field :salary %>
  <%= f.submit "Enviar" %>
<% end %>
  • Related