2016-01-04 58 views
1

我正在使用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 

我認爲它必須與職位動詞或我的路由問題,但我沒有耙:路線幾次都沒有取得進展。非常感謝你的幫助。我真的很感激它

回答

0

對名稱字段中顯示的電子郵件的回答是實際上將電子郵件保存到名稱字段中。從這個

改變電子郵件領域:

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

相關問題