【问题标题】:Mail_form : "No Route Matches [POST]" - Routing ErrorMail_form:“没有路由匹配 [POST]”- 路由错误
【发布时间】:2013-08-01 07:23:43
【问题描述】:

Rails 3.2

我使用 Mail_form gem(来自 plataformatec)为我的网站创建一个简单的“联系我们”表单。单击“发送”时,我收到一条路由错误消息:

Routing Error
No route matches [POST] "/contactus"
Try running rake routes for more information on available routes.

我有一个非常简单的设置,但我是 Rails 新手,并且仍然掌握它的窍门。我只希望表单向某个电子邮件地址发送电子邮件......没有别的。我知道问题出在 routes.rb 但我一直在摆弄这个问题太久了,我就是不知道出了什么问题。我从来没有为 Rails 错误而苦苦挣扎过。请帮忙!

“页面”模型:app/models/pages.rb

class Page < MailForm::Base
  attribute :name,          :validate => true
  attribute :email,         :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :page_title,    :validate => true
  attribute :page_body,     :validate => true

  def headers
    :subject => "#{page_title}",
    :to => "careers@example.com",
    :from => %("#{name}" <#{email}>)
  end
end

“页面”控制器:app/controllers/pages_controller.rb

class PagesController < ApplicationController
    respond_to :html

    def index
    end

    def create
        page = Page.new(params[:contact_form])
        if page.deliver
          redirect_to contactus_path, :notice => 'Email has been sent.'
        else
          redirect_to contactus_path, :notice => 'Email could not be sent.'
        end
    end
end

表单部分:app/views/pages/_form.html.erb

<%= simple_form_for :contact_form, url: contactus_path, method: :post do |f| %>

<div>
    <%= f.input :name %>
    <%= f.input :email, label: 'Email address' %>
    <%= f.input :page_title, label: 'Title' %>
    <%= f.input :page_body, label: 'Your message', as: :text %>
</div>

<div class="form-actions">
    <%= f.button :submit, label: 'Send', as: :text %>
</div>

查看(称为contactus):app/views/pages/contactus.html.erb

<body>
    <div>
        <h2 class="centeralign text-info">Contact Us</h2>
    </div>
    <div class="container centeralign">
            <%= render 'form' %>
    </div>
            <h2>We'd love to hear from you! </h2><br /><h4 class="muted">Send us a message and we'll get back to you as soon as possible</h4>
    </div>
</div>
</body>

Routes.rb

Example::Application.routes.draw do
  resources :pages
  root to: 'pages#index', as: :home
  get 'contactus', to: 'pages#contactus', as: :contactus
  get 'services', to: 'pages#services', as: :services

【问题讨论】:

  • 当您点击“发送”按钮时,它被视为您没有在 routes.rb 中定义任何路由的发布请求。

标签: ruby-on-rails ruby-on-rails-3 contact-form mail-form


【解决方案1】:

您的 routes.rb 文件没有POST /contactus 的路由 你有GET /contactus 的路线但没有POST,所以rails 说的是正确的。

添加类似的东西

post 'contactus', to: 'controller#action'

使用您需要调用的任何控制器和操作。或者,如果您尝试在页面控制器中调用 create 操作,那么您的问题是您在路由中添加了resources :pages,实际上您已经创建了路由

post 'pages'

因此,在这种情况下,我会将您的 simple_form_for 网址更改为发布到那里。尝试使用

simple_form_for :contact_form, url: pages_path, method: :post do

相反。如果pages_path 不起作用,那么只需在控制台中运行rake routes,您就会看到您拥有的所有路线的列表,包括它们的名称。然后选择你需要的那个:)

【讨论】:

  • 哇,你在一分钟内就回答了。谢谢!我在 pagescontroller#create 中收到“未初始化的常量”错误。关于为什么的任何想法?我确实在我的 Page.rb 模型中创建了一个“页面”类...
  • 啊,好吧,这取决于 Rails 约定。在 routes.rb 中,您也不需要使用 controller 这个词。所以把它改成pages#create应该没问题:)
  • 这对我来说很有意义!但它仍然说我在 pagescontroller#create .... 中有一个命名错误。
  • 即使您将其更改为 pages#create 而不是 pagescontroller#create?
  • 是的......错误告诉我它在页面控制器的“创建”方法中。但这很奇怪,因为我在 routes.rb 中添加了 post 'contactus', to: 'pages#create'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多