2015-07-21 151 views
2

我創建了一個登錄頁面,並且在根頁面上有兩種形式(試圖創建登錄頁面)。對軌道上的紅寶石非常新穎,所以原諒我,因爲我要解釋這非常可怕。Ruby on Rails表單提交錯誤和成功消息

登陸頁面控制器(landing_controller)看起來是這樣的:

class LandingController < ApplicationController 
    def index 
    @email = Email.new 
    @design = Design.new 
    end 
end 

的emails_controller(emails_controller)看起來是這樣的:

class EmailsController < ApplicationController 
protect_from_forgery with: :exception 

def new 
    @email = Email.new 
end 

def create 
    @email = Email.new(params[email_params]) 

    respond_to do |format| 
    if @email.save 
     format.html { redirect_to(root_path, :notice => 'Thank You For Subscribing!') } 
     format.json { render json: Email.create(email_params) } 
    else 
     format.html { redirect_to(root_path)} 
     format.json { render :json => @email.errors, :status => :unprocessable_entity } 
    end 
    end 
end 

    private 

    def email_params 
     params.require(:email).permit(:username, :email) 
    end 
end 

和設計控制器(designs_controller)看起來幾乎相同作爲emails_controller。

然後我在email.rb模型的一些驗證:

class Email < ActiveRecord::Base 
    validates :username, :presence => true 
    validates :email, :presence => true 
end 

又一次的design.rb看起來幾乎相同。

的形式我有目標網頁上(root_path)指數看起來是這樣的:

<%= form_for @email, url: emails_path, html: {class: "email-form"} do |f| %> 
    <% if @email.errors.any? %> 
     <h3>Error</h3> 
     <ul> 
      <% @email.errors.full_messages.each do |message| %> 
      <li><%= message %></li> 
      <% end %> 
     </ul> 
    <% end %> 
    <h2>Receive Notifications</h2> 
    <%= f.text_field :username, :class => 'email-box', :placeholder => "First Name", :autocomplete => :off %> 
    <%= f.text_field :email , :class => 'email-box', :placeholder => "Email", :autocomplete => :off %> 
    <%= f.submit "Subscribe", :class => 'email-submit' %> 
    <p class="info">- We'll update ​you ​when ​we launch our new website</p> 
    <% end %> 

當我提交表單打破了驗證我沒有錯誤,如果我提交表單後的驗證規則我不知道它是否在數據庫中創建了一個新條目。如果有人能幫助我,我會非常感激。

+1

有你的控制器和視圖之間的脫節。您沒有在任何地方顯示「成功」:「通知」,並且使用'redirect_to'將丟棄您重定向到的頁面上的@ email地址對象。我沒有看到你在表單上的任何地方使用'remote',所以JSON部分不可能被使用。 – sjagr

+0

您應該用'@email = Email.new(email_params)'替換'@email = Email.new(params [email_params])''。 'email_params'是一種從窗體返回允許的參數的方法。就發生的情況而言,您可以查看運行Rails服務器的終端,並且您應該看到日誌消息。你也可以看看'log/development.log'。最後,您應該熟悉Rails控制檯,您可以在其中查詢數據庫並查看已創建/更新的對象。 –

回答

2

您需要渲染着陸控制器索引操作而不是重定向到它。因爲在重定向時,它會執行@email = Email.new,並且所有錯誤都不會發送給電子郵件。試試這個在您的電子郵件控制器

def create 
    @email = Email.new(email_params) 

    respond_to do |format| 
    if @email.save 
     format.html { redirect_to root_path, notice: 'Thank You For Subscribing!' } 
     format.json { render json: Email.create(email_params) } 
    else 
     @design = Design.new 
     format.html { render "landing/index" } 
     format.json { render :json => @email.errors, :status => :unprocessable_entity } 
    end 
    end 
end 

成功或錯誤信息爲創建行動,把這個在您的application.html.erb

<% if flash[:error].present? %> 
    <p class='flash-error'><%= flash[:error] %></p> 
<% end %> 
<% if flash[:notice].present? %> 
    <p class='flash-notice'><%= flash[:notice] %></p> 
<% end %> 
+0

幫助成功和錯誤消息的鏈接:http://pupeno.com/2009/11/19/ensuring-the-displaying-of-flash-messages-in-ruby-on-rails/ – Athar

+0

嗯...獲取錯誤:http://prntscr.com/7vaii6 – Nesiehr

+0

沒關係,只是有一個不合適的括號。謝謝你的工作:) – Nesiehr