使用rails中的mail_form gem来更新联系人/新视图的联系人视图

问题描述:

我正在使用mail_form gem。我已经为联系表单创建了一个特定的控制器,并且每一个想法都是正确的,但是当我提交任何表单时,视图并不会从创建一个更改为创建一个,但是这个url会。它显示localhost3000/gmm/contacts(已经从localhost3000/gmm/contacts/new改变)。我很担心这个问题;此外,新的视图显示name's领域的电子邮件作为图像中显示:使用rails中的mail_form gem来更新联系人/新视图的联系人视图

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 => "[email protected]", 
    :to => "[email protected]", 
    :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 

我认为它必须与职位动词或我的路由问题,但我没有耙:路线几次都没有取得进展。非常感谢你的帮助。我真的很感激它

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

改变电子邮件领域:

<%= 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 
+0

非常感谢我有点困了,我已经改变了:命名到:电子邮件和现在的作品。谢谢 :) – jportella