質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

ログイン

ログインは、ユーザーがコンピューターシステムにアクセスするプロセスの事を呼びます。

DB2

DB2(IBM Database2)は、IBMのリレーショナルデータベース管理システム製品です。 UNIXとWindows、IBM社のメインフレームOS用が用意されており、 幅広いプラットフォームに対応しています。

Q&A

解決済

1回答

1886閲覧

ログイン機能実装(新規登録)のため複数ビュー・テーブルに分けてのDB保存 (ruby on rails)

ruorin

総合スコア5

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

ログイン

ログインは、ユーザーがコンピューターシステムにアクセスするプロセスの事を呼びます。

DB2

DB2(IBM Database2)は、IBMのリレーショナルデータベース管理システム製品です。 UNIXとWindows、IBM社のメインフレームOS用が用意されており、 幅広いプラットフォームに対応しています。

0グッド

0クリップ

投稿2019/11/27 12:04

前提・実現したいこと

ruby on rails 会員登録制のクローンアプリを作っています。
ログイン機能実装のため新規登録を複数ビュー・テーブルに分けてDBに保存させたい。

発生している問題・エラーメッセージ

新規登録時に入力する項目がDBに値が保存されません。
SignupControllerのcreateのif分岐で保存できずelseとなってしい保存できない、
binding.pryでみて見ると値は取れてきているのですが..
(バリテーションはまだ実装途中です。)

該当のソースコード

Rails.application.routes.draw do devise_for :users root 'items#index' resources :items, only: [:index, :new] resources :signup do collection do get 'step1' get 'step2' get 'step3' get 'step4' get 'done' end end end
class SignupController < ApplicationController def index end def step1 @user = User.new @user.build_profile end def step2 session[:nickname] = user_params[:nickname] session[:email] = user_params[:email] session[:password] = user_params[:password] session[:password_confirmation] = user_params[:password_confirmation] session[:profile_attributes_after_step1] = user_params[:profile_attributes] @user = User.new @user.build_profile end def step3 session[:profile_attributes_after_step2] = user_params[:profile_attributes] session[:profile_attributes_after_step2].merge!(session[:profile_attributes_after_step1]) @user = User.new @user.build_address end def step4 session[:address_attributes_after_step3] = user_params[:address_attributes] @user = User.new @user.build_profile @user.build_address end def create session[:profile_attributes_after_step4] = user_params[:profile_attributes] session[:profile_attributes_after_step4].merge!(session[:profile_attributes_after_step2]) @user = User.new( email: session[:email], password: session[:password], password_confirmation: session[:password_confirmation], nickname: session[:nickname] ) @user.build_address(session[:address_attributes_after_step3]) @user.build_profile(session[:profile_attributes_after_step4]) binding.pry if @user.save session[:id] = @user.id redirect_to done_signup_index_path else render '/signup/step1' end end private def user_params params.require(:user).permit( :email, :password, :password_confirmation, :nickname, profile_attributes: [:family_name, :first_name, :family_name_kana , :first_name_kana, :birthday, :mobile_phone,:card_number, :expiration_date, :security_code], address_attributes: [:zip_code, :prefecture, :city, :block, :building, :home_phone] ) end end
ActiveRecord::Schema.define(version: 2019_11_27_111930) do create_table "addresses", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "zip_code", null: false t.string "city", null: false t.string "block", null: false t.string "building", null: false t.string "home_phone", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "user_id" t.integer "prefecture" t.index ["user_id"], name: "index_addresses_on_user_id" end create_table "brands", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "categories", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "ancestry" t.index ["ancestry"], name: "index_categories_on_ancestry" end create_table "images", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "image_url", null: false t.bigint "item_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["item_id"], name: "index_images_on_item_id" end create_table "items", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name", null: false t.integer "price", null: false t.text "description", null: false t.integer "status", null: false t.integer "prefecture", null: false t.integer "fee", null: false t.integer "size", null: false t.integer "shipping", null: false t.integer "arrival", null: false t.integer "like", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "brand_id" t.bigint "size_id" t.bigint "category_id" t.bigint "user_id" t.index ["brand_id"], name: "index_items_on_brand_id" t.index ["category_id"], name: "index_items_on_category_id" t.index ["name"], name: "index_items_on_name" t.index ["size_id"], name: "index_items_on_size_id" t.index ["user_id"], name: "index_items_on_user_id" end create_table "profiles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "family_name", null: false t.string "first_name", null: false t.string "family_name_kana", null: false t.string "first_name_kana", null: false t.string "mobile_phone", null: false t.string "profile_image" t.text "profile_content" t.string "card_number" t.string "security_code" t.bigint "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "birthday" t.string "expiration_date" t.index ["user_id"], name: "index_profiles_on_user_id" end create_table "sizes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "size" t.bigint "category_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["category_id"], name: "index_sizes_on_category_id" end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "nickname", null: false t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end add_foreign_key "addresses", "users" add_foreign_key "images", "items" add_foreign_key "items", "brands" add_foreign_key "items", "categories" add_foreign_key "items", "sizes" add_foreign_key "items", "users" add_foreign_key "profiles", "users" add_foreign_key "sizes", "categories" end
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable has_one :profile, dependent: :destroy has_one :address, dependent: :destroy accepts_nested_attributes_for :profile accepts_nested_attributes_for :address devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :nickname, presence: true, length: { maximum: 20 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_REGEX } validates :password, length: { minimum: 6 , maximum: 128 } end
class Profile < ApplicationRecord belongs_to :user, optional: true VALID_PHONE_REGEX = /\A\d{10}$|^\d{11}\z/ validates :family_name, presence: true validates :first_name, presence: true validates :family_name_kana, presence: true validates :first_name_kana, presence: true validates :birthday, numericality: true validates :mobile_phone, presence: true, uniqueness: true, format: { with: VALID_PHONE_REGEX } end
class Address < ApplicationRecord #extend ActiveHash::Associations::ActiveRecordExtensions #belongs_to_active_hash :prefecture belongs_to :user, optional: true enum prefecture:{ 北海道:1,青森県:2,岩手県:3,宮城県:4,秋田県:5,山形県:6,福島県:7, 茨城県:8,栃木県:9,群馬県:10,埼玉県:11,千葉県:12,東京都:13,神奈川県:14, 新潟県:15,富山県:16,石川県:17,福井県:18,山梨県:19,長野県:20, 岐阜県:21,静岡県:22,愛知県:23,三重県:24, 滋賀県:25,京都府:26,大阪府:27,兵庫県:28,奈良県:29,和歌山県:30, 鳥取県:31,島根県:32,岡山県:33,広島県:34,山口県:35, 徳島県:36,香川県:37,愛媛県:38,高知県:39, 福岡県:40,佐賀県:41,長崎県:42,熊本県:43,大分県:44,宮崎県:45,鹿児島県:46,沖縄県:47 } VALID_ADDRESS_NUMBER_REGEX = /\A[0-9]{3}-[0-9]{4}\z/ VALID_PHONE_REGEX = /\A\d{10}$|^\d{11}\z/ validates :zip_code, presence: true, format: { with: VALID_ADDRESS_NUMBER_REGEX } validates :city, presence: true validates :block, presence: true validates :building, presence: true validates :home_phone, allow_blank: true, format: { with: VALID_PHONE_REGEX } end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

/signup/step1 のviewに
<% unless @user.errors.empty? %>
<%= @user.errors.full_massages %>
<% end %>
をいれると、何が原因でsaveに失敗したか、がわかります

投稿2019/11/27 12:39

winterboum

総合スコア23331

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ruorin

2019/11/28 04:11

birthdayの値が無効になっていることがわかりました! ご回答いただき、誠にありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問