私はまだRuby on Railsの新機能ですから、これを理解しようとしています。私はこれまで多くの研究を行ってきましたが、それを結び付ける方法を理解することはできません。私のUser & Profilesモデルはお互いに関連付けられ、プロファイルの保存を許可します。 - 私はDevises gemを使用しています.Devise gemは、Registrations Controllerを通じて新しいユーザーを登録します。 - プロファイルコントローラを作成しました。 - ユーザーが登録すると、自動的にProfile new.html.erbページに移動してプロファイルを設定します。しかし、私がそれを保存しようとすると、何も起こりません。しかし、ProfilesControllerの下にある 'belongs_to:users'コード行を削除すると、問題なく保存できますが、明らかにユーザーに関連付けられません。ユーザとプロフィールのモデルを関連付けることができません
- ユーザがプロファイルを1つだけ持つべき2つのモデルの関係を作成しました。プロファイルテーブルにuser_idを作成して、2つのテーブルをリンクする外部キーとしても機能させました。
マイUserモデル:
class User < ApplicationRecord
# Include default devise modules.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile, dependent: :destroy
accepts_nested_attributes_for :profile
end
マイプロフィールモデル:
class Profile < ApplicationRecord
belongs_to :user
#after_create :create_profile
end
私のスキーマ。
ActiveRecord::Schema.define(version: 20161126221219) do
create_table "profiles", force: :cascade do |t|
t.string "full_name"
t.string "contact_number"
t.string "location"
t.string "makeup_type"
t.string "bio"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "image"
end
create_table "users", force: :cascade do |t|
t.string "name", default: "", 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.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
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
end
マイプロファイルコントローラー:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
def index
@search = Profile.search(params[:q])
@profiles = @search.result(distinct: true)
end
def show
@profile = Profile.find(params[:id])
end
def new
@profile = Profile.new
end
def create
@profile = Profile.new(profile_params)
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: 'Your Profile was successfully created' }
format.json { render :show, status: :created, location: @profile }
else
format.html { render :new }
format.json { render json: @profile.errors, status: :unprocessable_entry }
end
end
end
def edit
@profile = Profile.find(params[:id])
end
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profile_url, notice: 'Profile was successfully destroyed.' }
format.json { head :no_content }
end
end
def set_profile
@profile = Profile.find(params[:id])
end
private
def profile_params
params.require(:profile).permit(:full_name, :contact_number, :location, :makeup_type, :bio, :user_id, :image)
end
end
工夫を設定するときに、私が作成されていないユーザーのコントローラを、作成したが、私はアクションの一部をオーバーライドしたかったです。しかし、これは、どこのメソッドをオーバーライドするのか、Deviseがユーザープロファイルを作成しているときにそれを行う方法がわからないため、私が立ち往生する場所です。とにかく、私のユーザコントローラは以下の通りです:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
end
最後に、私は以下のプロファイルnew.html.erbページへのルート登録ページをできるよう工夫登録コントローラを無効にするためにRegistrationsControllerを作成しました。
class RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
protected
# This allows a newly registered user to be directed to the Profile Creation page
def after_sign_up_path_for(resource)
new_profile_path(resource)
end
def after_sign_in_path_for(resource)
profile_path(resource)
end
end
ありがとうございました。私のプロファイルは実際にモデルの関連付けを変更せずに保存され、空のプロファイルで作成する前にuser_idが同じプロファイルレコードに表示されていたことがデータベースからわかりました。 – marg08