使用 rails 中的 mail_form gem 的 contacts/new 视图未更新联系人视图

Contacts view not updating from contacts/new view using the mail_form gem in rails

我正在使用 mail_form gem。我已经为联系表单创建了一个特定的控制器,一切似乎都是正确的,但是当我提交任何表单时,视图不会从创建视图更改为创建视图,但 url 会。它显示 localhost3000/gmm/contacts(已从 localhost3000/gmm/contacts/new 更改)。我很担心这个问题;此外,新视图在名称字段中显示电子邮件,如图所示:

"http://2.bp.blogspot.com/-Q4qUs1CHm-M/Voqc-VYiXII/AAAAAAAAAdE/nJMxgpTEu5s/s320/problem1.jpg"

    Controller file:

        class ContactsController < ApplicationController
def new
    @contact = Contact.new
end

def create
    @contact=Contact.new(params[:contact])
    @contact.request=request
    if @contact.deliver
        flash.now[:error]=nil
    else
        flash.now[:error]='Cannot send message.'
        render :new
    end
end

结束

新视图:

      <body>
<section>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 text-center">
                <h2 class="margin-top-0 wow fadeIn">Get in Touch</h2>
                <hr class="primary">
                <p>We love feedback. Fill out the form below and we'll get back to you as soon as possible.</p>
            </div>
            <div class="col-lg-10 col-lg-offset-1 text-center">
                <%= form_for @contact do |f| %>
                <div class="col-md-6 text-faded">
                    <%= f.label :name %>
                    <%= f.text_field :name, required: true, :class => "form-control", :placeholder => "Name"%>
                </div>
                <div class="col-md-6 text-faded">
                    <%= f.label :email %>
                    <%= f.email_field :name, required: true, :class => "form-control", :placeholder => "Email" %>
                </div>
                <div class="col-md-12 text-faded">
                    <%= f.label :message %>
                    <%= f.text_area :message, as: :text, :class => "form-control", :rows=>"9", :placeholder => "Your message here.."%>
                </div>
                <div class="hidden">
                    <%= f.label :nickname %><br>
                    <%= f.text_field :nickname, hint:"leave this field blank"%>
                </div>
                <div class="col-md-4 col-md-offset-4">
                    <%= f.submit "Send Message", :class=> "btn btn-primary btn-block btn-lg" %>
                </div>
                <%end%>
            </div>
        </div>
    </div>
</section>

型号:

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

def headers
    {
    :subject => "Contact Form",
    :to => "kurtco_91@hotmail.com",
    :to => "yomi.89gm@gmail.com",
    :from => %("#{name}" <#email>)
    }
end

结束

路线:

      Rails.application.routes.draw do

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

          get 'gmm/home' 

          get 'gmm/about'

          get 'gmm/services'

          get 'gmm/contact'

          get '/change_locale/:locale', to: 'settings#change_locale', as: :change_locale

我认为这一定是 post 动词或我的路由有问题,但我做了 rake: routes 几次,但没有任何进展。非常感谢你的帮助。非常感谢

对显示在名称字段中的电子邮件的回答是您实际上是将电子邮件保存到名称字段中。

从此更改电子邮件字段:

<%= f.email_field :name, required: true, :class => "form-control", :placeholder => "Email" %>

对此:

<%= f.email_field :email, required: true, :class => "form-control", :placeholder => "Email" %>

URL 问题的答案是:

根据我的评估,在您的控制器中,如果需要,您应该保存联系人。如果联系人保存成功,你应该重定向到 new_contact_path.

控制器应如下所示:

def create
@contact=Contact.new(params[:contact])
@contact.request=request
if @contact.deliver
    flash.now[:error]=nil
else
    flash.now[:error]='Cannot send message.'
    redirect_to new_contact_path
end
end