我正在爲我的用戶身份驗證和註冊使用設計。我可以註冊一個用戶沒有問題。我也使用friendly。我的問題是,我只能創建一個用戶配置文件。只能創建一個用戶配置文件 - Rails 4.2.4
設置...
user.rb:
class User < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
validates :name, uniqueness: true, presence: true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile # each user should have just one profile
end
profile.rb:
class Profile < ActiveRecord::Base
belongs_to :user
end
profiles_controller.rb:
class ProfilesController < ApplicationController
before_action :authenticate_user!
before_action :only_current_user
def new
# form where a user can fill out their OWN profile
@user = User.friendly.find(params[:user_id])
@profile = Profile.new
end
def create
@user = User.friendly.find(params[:user_id])
@profile = @user.build_profile(profile_params)
if @profile.save # Not saving!!
flash[:success] = 'Profile Created!'
redirect_to user_path(params[:user_id])
else
render action: :new # keeps rendering!
end
end
private
def profile_params
params.require(:profile).permit(:first_name, :last_name, :avatar, :job_title, :phone_number, :business_name)
end
end
爲什麼只有一個用戶可以創建配置文件而不是其他人?這與關係有關嗎?
你什麼錯誤? –
沒有錯誤。只是呈現新動作 – Sylar
如果沒有保存,則應該有錯誤。你可以在問題中發佈日誌嗎?也可以試試'if @ profile.save!' – Pavan