저는 아직 Ruby on Rails에 새로운 것이므로이 문제를 해결하려고합니다. 지금까지 많은 연구를 해왔지만 두 사용자 모두 & 프로필 모델이 서로 연결되어 내 프로필이 저장되도록 허용하는 방법을 알아낼 수 없습니다. - 등록 컨트롤러를 통해 새로운 사용자를 등록하는 Devise gem을 사용하고 있습니다. - 프로필 컨트롤러를 만들었습니다. - 사용자가 등록하면 자동으로 프로필 new.html.erb 페이지로 이동하여 프로필을 설정합니다. 그러나 그것을 저장하려고하면 아무 일도 일어나지 않습니다. 그러나 ProfilesController에서 'belongs_to : users'코드 줄을 제거하면 문제없이 저장할 수 있지만 분명히 사용자와 연결되지는 않습니다.사용자 및 프로필 모델을 연결할 수 없습니다.
- 나는 사용자가 단 하나의 프로필 만 가져야하는 2 가지 모델 간의 관계를 만들었습니다. Profiles 테이블에 user_id를 생성하여 2 개의 테이블을 연결하는 외래 키 역할을 수행했습니다.
내 사용자 모델 :
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