2015-07-21 152 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