Rails:我输入的每封电子邮件都已收到“电子邮件”

时间:2012-01-09 01:37:35

标签: ruby-on-rails

更新 检查控制台,即使出现此错误消息,很多用户实际上也会进入数据库!

即使更奇怪,您会注意到我成功保存到数据库后重定向到root_path。它永远不会去那里。这是日志。如果它保存,它有时会进入show动作,但实际上有一个时刻浏览器似乎卡住了,一切都空白了,我必须点击地址字段将其推送到show.index.html。

Redirected to http://localhost:3000root_path
Completed 302 Found in 34ms
[2012-01-08 18:08:27] ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000root_path (or bad hostname?)
    /Users/mel/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/uri/generic.rb:746:in `rescue in merge'
    /Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/uri/generic.rb:743:in `merge'
    /Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpresponse.rb:163:in `setup_header'
    /Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpresponse.rb:101:in `send_response'
    /Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:86:in `run'
    /Users/me/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

原始问题:

我正在为用户创建一个表单以注册赢取ipad。它有两个字段,电子邮件和名称。 当我提交表单时,无论我输入什么电子邮件,我都会收到相同的错误消息

1 error prohibited this from being saved:

There were problems with the following fields:

    Email has already been taken

奇怪的是,当我调试params时,它会显示我输入的任何新电子邮件地址。

utf8: "\xE2\x9C\x93"
authenticity_token: UgkHFLBfKYM2tZ11u6ItWvPS5XeTIMhFuJDwvhgqe30=
ipad: !map:ActiveSupport::HashWithIndifferentAccess 
  name: Amy
  email: franz@gmail.com
commit: Sign up
action: create
controller: ipads

任何人都有任何想法?我有点像菜鸟......

我只是将这段代码添加到我在Rails上制作的Enki博客......

我在ipad.rb模型上有这个验证(请注意,我在迁移中有一个Twitter地址列,我只是忽略了表单)

attr_accessible :name, :email, :twitter

validates :name, :presence => true,
         :length   => { :maximum => 50 }
validates :email, :presence => true,
                  :uniqueness => true

这是我的ipads_controller.rb

class IpadsController < ApplicationController

  def new
    @ipad = Ipad.new
    @title = "iPad Contest"
  end

  def create
    @ipad = Ipad.new(params[:ipad])
    if @ipad.save
      redirect_to 'root_path'
    else
      @title = "Poo"
      render 'new'
    end
  end


end

这是我的new.html.erb

<h1>Win an iPad</h1>
<%= @title %>

<h1>Sign up for iPad</h1>

<%= form_for(@ipad) do |f| %>
<%= render 'shared/errror_messages' %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>


<%= debug params %> 

1 个答案:

答案 0 :(得分:0)

使用redirect_to root_path而不是redirect_to'root_path'修复它。 Noob错误。