我还是Ruby on Rails的新手,所以我试图弄清楚这个。到目前为止,我已经做了很多研究,但无法弄清楚如何将它与我的用户和我的用户联系起来。配置文件模型相互关联并允许我的配置文件保存。 - 我正在使用Devise gem,它通过Registrations Controller注册一个新用户。 - 我创建了一个Profiles Controller。 - 当用户注册时,它们会自动进入Profile new.html.erb页面以设置其配置文件。但是,当我尝试保存它时,没有任何反应。但是,如果我删除了ProfilesController下的'belongs_to:users'代码行,那么我可以保存它而不会出现问题,但显然不会将其与用户关联。
我的用户模型:
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
我创建了一个Users控制器,在设置Devise时没有创建,但我想覆盖一些操作。然而,这是我卡住的地方,因为我不确定我应该覆盖哪些方法以及如何在Devise创建用户配置文件时这样做。无论如何,我的用户控制器如下:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
end
最后,我创建了一个RegistrationsController来覆盖Devise Registrations控制器,这样我就可以将注册页面路由到下面的Profiles 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
答案 0 :(得分:1)
您应该在控制器中使用current_user
帮助器。
而不是:
def profile_params
params.require(:profile).permit(:full_name, :contact_number, :location, :makeup_type, :bio, :user_id, :image)
end
您可以使用:
def profile_params
params[:profile][:user_id] = current_user.id
params.require(:profile).permit(:full_name, :contact_number, :location, :makeup_type, :bio, :user_id, :image)
end
此外,您应添加before_action
,以确保存在current_user
,否则您应该重定向到登录页面。