2015-10-05 70 views
1

我正在爲我的用戶身份驗證和註冊使用設計。我可以註冊一個用戶沒有問題。我也使用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 

爲什麼只有一個用戶可以創建配置文件而不是其他人?這與關係有關嗎?

+1

你什麼錯誤? –

+0

沒有錯誤。只是呈現新動作 – Sylar

+1

如果沒有保存,則應該有錯誤。你可以在問題中發佈日誌嗎?也可以試試'if @ profile.save!' – Pavan

回答

4

我們使用這種設置了一些我們的應用程序 - User - >Profile

總之,您應該在用戶創建時構建配置文件。然後您可以根據需要編輯配置文件。你有Profile.new方法的問題是非常低效的...


#app/models/user.rb 
class User < ActiveRecord::Base 
    has_one :profile 
    before_create :build_profile #-> saves blank associated "Profile" object after user create 
end 

這將意味着每次User創建,其對應的Profile對象也被附加到數據庫。

這會給你編輯的配置文件的能力要求:

#config/routes.rb 
resources :users, path_names: { edit: "profile", update: "profile" }, only: [:show, :edit, :update] 

這會給你使用以下機會:

#app/controllers/users_controller.rb 
class UsersController < ApplicationController 
    before_action :authenticate_user!, only: [:edit, :update] 
    before_action :authorize, only: [:edit, :update] 

    def show 
     @user = User.find params[:id] 
    end 

    def edit 
     @user = current_user 
    end 

    def update 
     @user = current_user 
     @user.update user_params 
    end 

    private 

    def authorize 
     id = params[:id] 
     redirect_to user_show_path(id) if current_user.id != id #-> authorization 
    end 

    def user_params 
     params.require(:user).permit(:x, :y, :z, profile_attributes: [:homepage, :other, :profile, :attributes]) 
    end 
end 

查看/形式將是以下:

#app/views/users/edit.html.erb 
<%= form_for @user do |f| %> 
    <%= f.fields_for :profile do |f| %> 
     <%= f.text_field :homepage %> 
     ... 
    <% end %> 
    <%= f.submit %> 
<% end %> 

問候你當前設置:

def new 
    @profile = current_user.profile.new 
end 

def create 
    @profile = current_user.profile.new profile_params 
    if @profile.save 
     redirect_to user_path(params[:id]), notice: "Profile Created!" 
    else 
     render action: :new 
    end 
end 

private 

def profile_params 
    params.require(:profile).permit(:x, :y, :z) 
end 
+0

我喜歡你的user.rb設置。這個'before_create:build_profile'有很大的不同。謝謝。 – Sylar

+0

Np,感謝upvote。 –

0

不確定爲何不在用戶的after_create事件中創建配置文件。一旦創建用戶 - 創建一個空的(但關聯的)配置文件。

class User 
    has_one :profile, dependent: :destroy 
    after_create { 
    build_profile unless profile 
    profile.save 
    } 
end 

class Profile 
    belongs_to :user, autosave: true 
end 

然後,在你的控制器中,你只需要更新方法。

def update 
    if current_user.profile.update_attributes(user_params) 
    flash_it :success 
    return redirect_to edit_user_profile_path 
    else 
    flash_it :error 
    render :edit 
    end 
end 
相關問題