【问题标题】:Setting up a simple Rails 5 Mailform设置一个简单的 Rails 5 邮件表单
【发布时间】:2017-02-04 12:24:36
【问题描述】:

我已经有一段时间没有在 Rails 中编程了……了解所有 Rails 5.0 语法和更改。

使用 Rails 5.0.0.1

使用 Ruby ruby​​ 2.3.1p112(2016-04-26 修订版 54768)[x86_64-darwin16]

我正在尝试在登录页面上设置一个简单的联系我们表单。我打算直接从表单发送电子邮件而不是将其存储到数据库中。

我正在使用mail_form gem 并关注此thread

我知道我在控制器上犯了一些新手错误,但是在遵循了几次 Stack Q/A 之后,我仍然没有完全做到。

模型在 Rails 控制台中成功发送电子邮件。我只是无法让控制器工作。这是一个单页网站,因此我将部分内容添加到 Pages View 文件夹中的索引页面。

我遇到了错误

AbstractController::ActionNotFound (The action 'create' could not be found for PagesController):

路线

 Rails.application.routes.draw do
  get 'users/new'
  resources :pages
  root 'pages#index'
end

表格部分

app/views/pages/_form.html.erb

<%= form_tag(pages_path)  do %>
    <div class="row">
        <div class="column width-6">
            <%= text_field_tag 'firstname', nil, class: 'form-element rounded large', placeholder: 'First Name*', tabindex: '1' %>
        </div>
        <div class="column width-6">
            <%= text_field_tag 'lastname', nil, class: 'form-element rounded large', placeholder: 'Last Name*', tabindex: '2' %>
        </div>
        <div class="column width-6">
            <%= email_field_tag 'email', nil, class: 'form-element rounded large', placeholder: 'Email Address*', tabindex: '3' %>
        </div>
        <div class="column width-6">
            <%= text_field_tag 'website', nil, class: 'form-element rounded large', placeholder: 'Website', tabindex: '4' %>
        </div>
        <div class="column width-6">
            <%= text_field_tag 'phone', nil, class: 'form-element rounded large', placeholder: 'Phone', tabindex: '5' %>
        </div>
    </div>
    <div class="row">
        <div class="column width-12">
            <%= text_area_tag 'message', nil, class: 'form-element rounded large', placeholder: 'Message*', tabindex: '6' %>
        </div>
        <div class="column width-12">
            <%= submit_tag 'Send Email', class: 'form-submit button rounded medium bkg-theme bkg-hover-green color-white color-hover-white'  %>
        </div>
    </div>
<% end %>

页面控制器

class PagesController < ApplicationController
  def index
    @contact = Page.new(params[:page])
    if @contact.deliver
      redirect_to :back, :notice => "Thank you for contacting us, We'll get back to you shortly!"
    else
      flash.now[:error] = 'Sorry, it looks like there was an error with your message, Please give us a call or shoot us a text at ....'
    end
  end
end

感谢您的帮助。这个社区太棒了!

【问题讨论】:

  • 您需要将resources :pages 添加到您的routes.rb 文件中

标签: ruby-on-rails controller ruby-on-rails-5 mail-form


【解决方案1】:

页面控制器缺少您的路线。

config/routes.rb 添加:

resources :pages

在 PagesController.rb 中

class PagesController < ApplicationController
  def create
    @contact = Page.new(params[:page])
    if @contact.deliver
      redirect_to :back, :notice => "Thank you for contacting us, We'll get back to you shortly!"
    else
      flash.now[:error] = 'Sorry, it looks like there was an error with your message, Please give us a call or shoot us a text at ....'
    end
  end
end

处理 AJAX posts。

【讨论】:

  • 我更改了路由...但随后出现以下错误...AbstractController::ActionNotFound(找不到 PagesController 的“创建”操作):
  • 你正在学习 Rails,继续前进。我真的建议阅读guides.rubyonrails.org/routing.html - 在您的终端运行rake routes,您会看到创建了7条路线并且您的表单指向create而不是index...更改您的名称控制器中的方法从 indexcreate 并且它将起作用。
【解决方案2】:

redirect_to :back 在 rails 5 中已弃用。取而代之的是一个名为 redirect_back 的新函数。

但我不会使用index 操作来创建新页面,即使您不将其保存到数据库中。相反,我会定义一个名为create 的新操作,并最终重定向到index。由于您已经在路线中使用了resources :pages,因此您无需在其中添加任何内容。在这里您可以找到默认路由及其操作,以及它们的用途:http://edgeguides.rubyonrails.org/routing.html#resource-routing-the-rails-default

如果您正在使用模型,我也会考虑使用form_for 而不是form_tag。在这里您可以找到一个简单的示例:http://edgeguides.rubyonrails.org/getting_started.html#the-first-form

我希望这会有所帮助:)

【讨论】:

  • 谢谢佐拉。这是一个简单的营销页面,所以我试图避免设置数据库,从而避免 form_for... 似乎有点矫枉过正。
猜你喜欢
  • 1970-01-01
  • 2017-07-25
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多