我還是比較新的Ruby on Rails,所以我試圖找出這一個。到目前爲止,我已經進行了大量研究,但無法弄清楚如何將其綁定在一起,以便我的兩個用戶配置文件模型彼此關聯並允許我的配置文件保存。 - 我正在使用Devise gem,它通過註冊控制器註冊一個新用戶。 - 我創建了一個配置文件控制器。 - 用戶註冊時,它們會自動進入配置文件new.html.erb頁面以設置其配置文件。但是,當我嘗試保存它時,沒有任何反應。但是,如果我刪除ProfilesController下的'belongs_to:users'代碼行,那麼我可以保存它沒有問題,但它顯然不會將其與用戶關聯。無法關聯用戶和配置文件模型
- 我創建了2個模型之間的關係,其中用戶應該只有1個配置文件。我還在「個人檔案」表中創建了一個user_id,以充當鏈接這兩個表的外鍵。
我的用戶模型:
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
最後,我創建了一個RegistrationsController覆蓋設計註冊記憶控制器,以便我能航線的註冊頁面下方的個人資料頁面new.html.erb。
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