使用无表格实现验证注册表单时出错

时间:2014-04-01 19:57:59

标签: ruby-on-rails forms validation simple-form

我正在尝试实施注册表单。见this screenshot。我正在使用LearnRails tutorial来帮助我。

当您输入有效的电子邮件地址时,它会起作用。但是,如果您没有输入有效的电子邮件地址,则会出现此错误:undefined method 'empty?' for nil:NilClass。我的日志说:

ActionView::Template::Error (undefined method `name' for nil:NilClass):
    1: <% content_for(:title, "#{@college.name} Student Reviews" + " | #{params[:section1]} | #{params[:section2]}".titleize) %>
    2: <% description "#{@question}" %>
    3: 
    4: <div id="college_pages_css">
  app/views/college_pages/disqus_normal.html.erb:1:in `_app_views_college_pages_disqus_normal_html_erb___3963356040782610986_70269489097160'

这很奇怪,因为它应该重定向到我的主页,而主页没有@college变量。

注意:我正在使用activerecord-tableless gem,因为教程使用它。

模型

class Subscriber < ActiveRecord::Base
    attr_accessible :email
    has_no_table
    column :email, :string
    validates_presence_of :email
    validates_format_of :email,
        :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i

    def subscribe
        mailchimp = Gibbon::API.new(ENV['MAILCHIMP_API_KEY'])
        result = mailchimp.lists.subscribe({
            :id => ENV['MAILCHIMP_LIST_ID'],
            :email => {:email => self.email},
            :double_optin => false,
            :update_existing => true,
            :send_welcome => true
        })
        Rails.logger.info("Subscribed #{self.email} to MailChimp") if result
    end
end

控制器

class SubscriberController < ApplicationController

  def create
    @subscriber = Subscriber.new(params[:subscribe])
    if @subscriber.valid?
        @subscriber.subscribe
        redirect_to root_path
    else
        render root_path
    end
  end
end

查看

<%= simple_form_for :subscribe, url: 'subscribe' do |f| %>
    <%= f.input :email, label: false %> <br/>
    <%= f.button :submit, "Notify me", class: "btn btn-primary" %>
<% end %>

请注意,在我使用secure_params时,本教程使用attr_accessible方法。我不认为这会是一个问题,但它是可能的。

我在考虑忽略这个问题,只是使用客户端验证,但这会导致我的网站崩溃。关于客户端验证的主题,不是HTML5电子邮件输入字段应该自动验证吗?

如何解决此问题?

编辑:加载主页时记录

Started GET "/" for 127.0.0.1 at 2014-04-01 16:15:47 -0400
Processing by StaticPagesController#home as HTML
  Rendered static_pages/home.html.erb within layouts/application (3.6ms)
Completed 200 OK in 41ms (Views: 40.3ms | ActiveRecord: 0.0ms)


Started GET "/assets/jquery.ui.theme.css?body=1" for 127.0.0.1 at 2014-04-01 16:15:47 -0400


Started GET "/assets/jquery.ui.accordion.css?body=1" for 127.0.0.1 at 2014-04-01 16:15:47 -0400
.
.
.
Started GET "/favicon.ico" for 127.0.0.1 at 2014-04-01 16:15:49 -0400


Started GET "/favicon/academics/professors/1" for 127.0.0.1 at 2014-04-01 16:15:50 -0400
Processing by CollegePagesController#disqus_normal as */*
  Parameters: {"college"=>"favicon", "section1"=>"academics", "section2"=>"professors", "question_id"=>"1"}
  College Load (1.4ms)  SELECT "colleges".* FROM "colleges" WHERE "colleges"."url" = 'favicon' LIMIT 1
  Rendered college_pages/disqus_normal.html.erb within layouts/application (3.7ms)
Completed 500 Internal Server Error in 14ms

ActionView::Template::Error (undefined method `name' for nil:NilClass):
    1: <% content_for(:title, "#{@college.name} Student Reviews" + " | #{params[:section1]} | #{params[:section2]}".titleize) %>
    2: <% description "#{@question}" %>
    3: 
    4: <div id="college_pages_css">
  app/views/college_pages/disqus_normal.html.erb:1:in `_app_views_college_pages_disqus_normal_html_erb___3963356040782610986_70269489097160'


  Rendered /Users/adamzerner/.rvm/gems/ruby-2.0.0-p451@global/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (80.6ms)
  Rendered /Users/adamzerner/.rvm/gems/ruby-2.0.0-p451@global/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.6ms)
  Rendered /Users/adamzerner/.rvm/gems/ruby-2.0.0-p451@global/gems/actionpack-4.0.3/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (98.3ms)


Started GET "/assets/favicon.ico" for 127.0.0.1 at 2014-04-01 16:15:50 -0400

disqus_normal行动

def disqus_normal
  @college = College.find_by_url(params[:college])
  @question = get_question(params[:section2], params[:question_id])
end

1 个答案:

答案 0 :(得分:1)

错误

undefined method 'name' for nil:NilClass

出现在第1行

<% content_for(:title, "#{@college.name} Student Reviews" + " | #{params[:section1]} | #{params[:section2]}".titleize) %>

college_pages/disqus_normal.html.erb页的

这意味着@college is nil并且您正在尝试访问name对象上的属性nil。因此,错误。

要解决此问题,请确保在您重定向到此页面的操作中设置@college实例变量的值。

<强>更新

此外,路由存在问题。

get '/:college', to: redirect('/%{college}/academics/professors/1')

由于上述有问题的路线,GET“/favicon.ico”正在转换为GET“/ favicon / academics / professors / 1”,并且正在调用disqus_normal。理想情况下,如果向有问题的路线添加一些静态文本会更好。

例如:

get '/college/:college', to: redirect('/%{college}/academics/professors/1')