我是 Ruby on Rails 的新手,作为我的第一个项目,我正在为我的初创公司创建一个 beta 注册页面。我只想将用户的电子邮件地址保存到数据库中以供将来使用,并且我无法将任何数据保存到我的数据库中。我可以通过 rails 控制台添加电子邮件,但我的表单/控制器无法正常工作。怎么了?
用户模型:
class User < ActiveRecord::Base
attr_accessible :email
before_save { |user| user.email = email.downcase }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness:
{case_sensitive: false }
end
用户控制器:
class UsersController < ApplicationController
def index
end
def create
User.create params[:email]
redirect_to :back, :notice => "Success!"
end
end
主页 HTML:
<h1>######</h1>
<p>Welcome to #####! Give us your email address, and we'll keep you informed on our
latest happenings.
You'll also be placed on the list for our private alpha and beta testings.</p>
<%= render 'form' %>
形成部分:
<%= form_for User.new do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.submit %>
<% end %>
谢谢!