私は Ruby on Rails を初めて使用し、最初のプロジェクトとして、スタートアップのベータ サインアップ ページを作成しています。将来の使用のためにユーザーの電子メールアドレスをデータベースに保存したいだけですが、データベースにデータを保持することはできません。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 %>
ありがとう!