私のログ ファイルが示すように、アプリでフォームを送信すると、パラメーターは正しく設定されていますが、データベースに保存されていません。これは、ドロップダウン フォームで使用される 2 つのパラメーターでのみ発生します。
ここに私のロガーがあります:
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"tIdsWBUB+ik8eeOOxUEQs9mSB/WiSyX2Gkw3/fAw64w=", "user"=> {"name"=>"Jeffgo", "email"=>"JeffreyEricKatz6@gmail.com", "cell_number"=>"5554595515", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "day_open"=>"0", "time_open"=>"14"}, "commit"=>"Create my account"}
RecordedLesson Load (0.1ms) SELECT "recorded_lessons".* FROM "recorded_lessons" LIMIT 1
(0.1ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('JeffreyEricKatz6@gmail.com') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
SQL (51.2ms) INSERT INTO "users" ("cell_number", "created_at", "day_open", "email", "last_class", "name", "password_digest", "recorded_lesson_id", "test", "time_available", "time_open", "type", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["cell_number", "2154997415"], ["created_at", Sun, 28 Oct 2012 16:45:50 EDT -04:00], ["day_open", nil], ["email", "jeffreyerickatz6@gmail.com"], ["last_class", 0], ["name", "Jeffgo"], ["password_digest", "$2a$10$Idl8H7tMvotqhwiFteya2eSUaNEdAPNU6dqrp5PPalEyfo7w6x5gq"], ["recorded_lesson_id", 1], ["test", nil], ["time_available", nil], ["time_open", nil], ["type", nil], ["updated_at", Sun, 28 Oct 2012 16:45:50 EDT -04:00]]'
では、フォームの一部を次に示します: <% provide(:title, "Sign up") %>
サインアップ
<div class="row">
<div class="span6 offset3">
<%= simple_form_for @user do |f| %>
<%= render 'shared/user_error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :cell_number %>
<%= f.text_field :cell_number, :hint => '5555555555' %>
<%= f.label :password %><br />
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
<%= f.label :day_open %> <br />
<%= f.select("day_open", {"Sunday" => "0", "Monday" => "1", "Tuesday" => "2", "Wednesday" => "3", "Thursday" => "4", "Friday" => "5", "Saturday" => "6"}) %>
<%= f.label :time_open %> <br />
<%= f.select("time_open", {"7:30 pm" => "14", "8:30 pm" => "15", "9:30 pm" => "16"}) %>
<div class="actions">
<%= f.submit "Create my account" %>
</div>
<% end %>
</div>
</div>
私のコントローラー:
class UsersController < ApplicationController
def new
@title = "Sign up"
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
@title = "Sign up"
render 'new'
end
end
end
最後に、ここに私のモデルがあります:
class User < ActiveRecord::Base
has_many :tutoring_sessions
belongs_to :recorded_lesson
attr_accessor :day_open, :time_open
attr_accessible :name, :email, :cell_number, :password, :password_confirmation,
:day_open, :time_open, :tutoring_sessions_attributes, :recorded_lesson_id
has_secure_password
accepts_nested_attributes_for :tutoring_sessions, :allow_destroy => true
before_save { |user| user.email = email.downcase }
validates :name, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false}
validates :password, presence: true, length: { minimum: 6}, on: :create
validates :password_confirmation, presence: true, on: :create
after_initialize :init
def init
self.recorded_lesson_id ||= RecordedLesson.first.id
self.last_class ||= 0
end
def advance
if self[:recorded_lesson_id] == nil
self[:recorded_lesson_id] = RecordedLesson.first.id
else
self[:recorded_lesson_id] += 1
end
#self.save
end
def nows_weekday
@todays_weekday = Time.now.utc.wday
end
def number_of_days_from_now
(selected_day - nows_weekday)
end
def is_student?
self[:type] == 1
end
def is_tutor?
self[:type] == 0
end
def set_day_and_time_available
set_hour = Time.now.utc.change(:hour => selected_time)
set_day = set_first_day_of_session(set_hour)
self[:time_available] ||= set_day.to_datetime
end
def set_first_day_of_session(set_hour)
if selected_day > nows_weekday #(Eg. Today is Tuesday and chooses wednesday)
first_day_of_session= set_hour.advance(:days => number_of_days_from_now)
else #(Eg. Today is Tuesday and chooses Monday)
first_day_of_session= set_hour.advance(:days => (number_of_days_from_now + 7))
end
end
end