Rails ActionController::RoutingError(没有路由匹配 [POST] "/"):

Rails ActionController::RoutingError (No route matches [POST] "/"):

rails6.0.3

我已经使用 gem 'mail_form'.

设置了静态邮件表单

我知道它工作正常,因为我可以从 rails 控制台发送电子邮件。

问题出在表单上,​​当我按提交时,出现路由错误。

ActionController::RoutingError (No route matches [POST] "/"):

首页表格

<div class="container">
  <%= form_with model: @contact do |f| %>
    <div class="mb-3">
      <h3><%= f.label :name, class: "label" %></h3>
      <%= f.text_field :name, class: "form-control", placeholder: "Write name in here" %>
      <%= f.text_field :email, class: "form-control", placeholder: "Write email in here" %>
      <%= f.text_field :message, class: "form-control", placeholder: "Write message in here" %>
      <%= f.submit %>
    </div>
  <% end %>
</div>

routes.rb

resources :contact, only: [:create]

contact.rb

class Contact < MailForm::Base
  attribute :name, validate: true
  attribute :email, validate: true
  attribute :message
  def headers
    {
      subject: "My Contact Form",
      to: 'myemail@gmail.com',
      from: %("#{name}" <#{email}>)
    }
  end
end

contact_controller.rb

class ContactController < ApplicationController
  def create
    @contact = Contact.new()
    @contact.name = params[:name]
    @contact.email = params[:email]
    @contact.message = params[:message]
    if @contact.deliver

      render json: {message: "Email sent successfully"}
      redirect_to root_path
    else
      render json: @contact.errors
    end
  end
end

我做错了什么或错过了哪一步?

已编辑控制器

class ContactController < ApplicationController

  def create
    @contact = Contact.new(contact_params)
    if @contact.deliver
      redirect_to root_path
    else
      render json: @contact.errors
    end
  end

  private
  def contact_params
    params.require(:contact).permit(:name, :email, :message)
  end
end

已编辑表格

<div class="container">
  <%= form_for Contact.new, url: contacts_path do |f| %>
    <div class="mb-3">
      <h3><%= f.label :name, class: "label" %></h3>
      <%= f.text_field :name, class: "form-control", placeholder: "Write name in here" %>
      <%= f.text_field :email, class: "form-control", placeholder: "Write email in here" %>
      <%= f.text_field :message, class: "form-control", placeholder: "Write message in here" %>
      <%= f.submit %>
    </div>
  <% end %>
</div>

已编辑routes.rb

resources :contacts, only: [:create]
resources :contacts, only: [:create]

资源应该是复数。

class ContactsController < ApplicationController
  def create
    @contact = Contact.new(contact_params)
    if @contact.deliver
      # You can't both render and redirect 
      render json: { message: "Email sent successfully" }
    else
      render json: @contact.errors
    end
  end

  private
  def contact_params
    params.require(:contact)
          .permit(:name, :email, :message)
  end
end

参数嵌套在params[:contact][:name]params[:contact][:email]等中。使用strong parameters将参数列入白名单以进行批量分配,而不是充当人工编译器。

@form不能自动解析路径,我觉得:

 <%= form_with model: @contact do |f| %>
  • 尝试添加明确的 url:form_with model: @contact, url: contacts_path
  • 还要确保控制器总是复数,就像@max说的,如果使用resourceresources,否则迟早会Rails 从我的经验中感到困惑。另一方面,您的联系人只有一种方法,您可以为此添加一个手动路由,不要依赖 Rails 自动命名魔法:
# routes

  post "/contact"

# this should also generate a `contact_path` helper, which you can use in the