2016-04-01 28 views
1

我有模型用戶與關係:如何在佈局中顯示一個變量?

belongs_to :freelancer 

和模型自由職業者用:

belongs_to :user. 

對於登記使用設計。 註冊後,將自動創建一個新用戶,並自行創建user_id = current user.id的Freelancer對象。我還有什麼更好的方式獲得與user_id = current user@freelancer對象後註冊登錄並通過在佈局模板:

佈局/ _user_panel.html.erb

<div class="user-panel_head"> 
    <%= link_to @freelancer, title: current_user.username, class: "user-panel__avatar active" do %> 
     <%= image_tag "default/avatar.png", class: "avatario" %> 
    <% end %> 
    <div class="user-panel__side"> 
     <%= link_to current_user.username, @freelancer, class: "user-panel__user-name" %> 
     <span class="btn_drop icon_arrow_up" role="expand_menu_trigger"></span> 
    </div> 
</div> 

UPDATE:

# == Schema Information 
# 
# Table name: users 
# 
# id      :integer   not null, primary key 
# email     :string   default(""), not null 
# encrypted_password  :string   default(""), not null 
# reset_password_token :string 
# reset_password_sent_at :datetime 
# remember_created_at :datetime 
# sign_in_count   :integer   default(0), not null 
# current_sign_in_at  :datetime 
# last_sign_in_at  :datetime 
# current_sign_in_ip  :string 
# last_sign_in_ip  :string 
# created_at    :datetime   not null 
# updated_at    :datetime   not null 
# confirmation_token  :string 
# confirmed_at   :datetime 
# confirmation_sent_at :datetime 
# unconfirmed_email  :string 
# failed_attempts  :integer   default(0), not null 
# unlock_token   :string 
# locked_at    :datetime 
# username    :string 
# 
# Indexes 
# 
# index_users_on_confirmation_token (confirmation_token) UNIQUE 
# index_users_on_email     (email) UNIQUE 
# index_users_on_reset_password_token (reset_password_token) UNIQUE 
# index_users_on_unlock_token   (unlock_token) UNIQUE 
# 

class User < ApplicationRecord 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    belongs_to :freelancer 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable 
end 

FREELANCER MODEL

# == Schema Information 
# 
# Table name: freelancers 
# 
# id    :integer   not null, primary key 
# first_name  :string 
# last_name  :string 
# rate   :integer 
# birthday  :date 
# location  :string 
# description :text 
# site   :string 
# visible  :boolean 
# avatar   :string 
# category_id :integer 
# created_at  :datetime   not null 
# updated_at  :datetime   not null 
# user_id  :integer 
# specialization :string 
# price_category :string 
# ownership  :string 
# 
# Indexes 
# 
# index_freelancers_on_category_id (category_id) 
# index_freelancers_on_user_id  (user_id) 
# 

class Freelancer < ApplicationRecord 
    belongs_to :category 
    belongs_to :user 
    has_many :links 
    has_and_belongs_to_many :payment_options 

    accepts_nested_attributes_for :links, allow_destroy: true 

    PRICE_CATEGORIES = ['Project', 'Hour', 'Month', 'For 1000 characters'] 
    OWNERSHIP_TYPES = ['Individual', 'Company'] 

回答

1

試試這個,它會在sign_up之後立即創建Freelancer

class User < ActiveRecord::Base 
    has_one :freelancer, dependent: :destroy 
    before_create :set_freelancer 

    def set_freelancer 
    build_freelancer(id: self.id, user_id: self.id, email: self.email) 
    end 
end 

class Freelancer < ActiveRecord::Base 
    belongs_to :user 
end 
+0

如果你有它的麻煩,只是現在應該固定工作。這也是很好的一點,用戶和自由職業者的ID將永遠是一樣的。 – 7urkm3n

1

這個例子中current_user@freelancer有什麼區別?你用來自current_user的數據填充你的視圖,這意味着它們是相同的對象,所以你不能只是link_to current_user, ...

無論如何,要解決您的問題 - 控制器中的實例變量將傳遞給佈局,就像它們對模板和部分進行操作一樣。他們都被認爲是意見,並且一般都以類似的方式表現。因此,您需要在控制器中設置@freelancer,其中包括負責渲染頁面的任何操作。

對於Devise,您應該考慮覆蓋after_sign_up_path_for方法並返回您希望用戶在註冊後重定向到的路線。

class Users::RegistrationsController < Devise::RegistrationsController 

    # Resource is a User in this case 
    def after_sign_up_path_for(resource) 
     super(resource) 

     user_path(resource) # Return the path for the `users#show` route 
    end 
    end 
end 

所以,你會在與user_path相關的控制器動作分配@freelancer。同樣的原則適用於Devise's SessionsController

+0

謝謝你,安東尼 –

相關問題