2012-01-30 17 views
1

我創建具有以下屬性的微柱模型:顯示微博中的鏈接和鏈接到作者個人資料的評論?

<Micropost id: 1, content: "test", user_id: 1, created_at: "2012-01-25 15:34:30", updated_at: "2012-01-29 11:07:53", title: "asdasdad"> 

一個用戶模型具有以下屬性:

<User id: 1, email: "[email protected]", username: nil, etc...> 

和具有以下屬性評論模型:

<Comment id: 1, content: "sdf", micropost_id: 1, user_id: nil, created_at: "2012-01-29 11:10:42", updated_at: "2012-01-29 11:10:42"> 

到目前爲止,我只做到這一點:

  • 一個顯示的微柱筆者的用戶名
  • 顯示微觀柱評論的作者的ID

    <h2>Micropost Index</h2> 
    <% @microposts.each do |micropost| %> 
    
    <%= micropost.title %></td> 
    <%= micropost.content %></td> 
    <%= link_to 'Show', micropost %></td> 
    <%= link_to 'Edit', edit_micropost_path(micropost) %></td> 
    <%= link_to 'Destroy', micropost, confirm: 'Are you sure?', method: :delete %>  
    
    <h2>Comments</h2> 
    
    <% @micropost.comments.each do |comment| %> 
        <p> 
        <b>Comment:</b> 
        <%= comment.content %> 
        </p> 
        <p> 
        <b>Commenter</b> 
        <%= comment.user_id %> 
        </p> 
    <% end %> 
    
  • 我不知道如何創建到作者個人資料的鏈接(例如, mysite.com/users/1)。

  • 我沒怎麼檢索評論的作者的姓名和聯繫到他/她的個人資料

編輯:

型號:

用戶。 rb:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, 
     :omniauthable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :username 

    has_many :microposts 
    has_many :comments 

    def self.find_for_facebook_oauth(access_token, signed_in_resource=nil) 
    data = access_token.extra.raw_info 
    if user = User.where(:email => data.email).first 
     user 
    else # Create a user with a stub password. 
     User.create!(:email => data.email, :password => Devise.friendly_token[0,20]) 
    end 
    end 
end 

micropost.rb:

class Micropost < ActiveRecord::Base 
    attr_accessible :title, :content 

    belongs_to :user 
    has_many :comments 
end 

comment.rb:

class Comment < ActiveRecord::Base 
    attr_accessible :content, :user_id 

    belongs_to :micropost 
    belongs_to :user 
end 

控制器微柱:

控制器/ microposts.rb

def show 
    @micropost = Micropost.find(params[:id]) 
    end 

    def new 
    @micropost = Micropost.new 
    end 

    def create 
    @user = current_user 
    @micropost = @user.microposts.new(params[:micropost]) 
    @micropost.save 
    redirect_to @micropost 
    end 

任何建議來完成這個?

回答

2

爲了做一個鏈接到用戶可以使用

<%= link_to comment.user.username, comment.user %> 

在「Rails的魔術」一般的部分是,你,如果你設置了關聯正確,您可以通過訪問相關對象點符號。這意味着,您不需要說comment.user_id,而是直接轉到相關的用戶對象,例如, comment.user.usernamecomment.user.email ...你的想法:)

爲了這個你應該已經建立了你的模型是這樣的:

class User < ActiveRecord::Base 
    validates_presence_of :username #username should obviously not allow nil values 
    has_many :microposts 
    has_many :comments 
end 

class MicroPost < ActiveRecord::Base 
    belongs_to :user 
end 

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

感謝您的回答。請檢查我的**編輯**我包括我的模型。但出於某種原因,我得到這個:'nilClass'的未定義方法'用戶名' – alexchenco 2012-01-30 10:13:55

+0

奇怪但只有'comment.user'工程(顯示:'#')。是否因爲我需要在註釋表中設置外鍵(例如user_username)? – alexchenco 2012-01-30 10:20:28

+0

這可能是因爲你沒有在你的'MicroPost'實例中設置user_id。檢查你的'@ micropost'來看看user_id是否爲空。如果爲空,請檢查您的控制器以確認您使用了諸如'@ user.microposts.create(...)'或類似的東西。 – 2012-01-30 10:30:44

1
# Link: 
=link_to "User profile", user_path(comment.user) 

# Name of the author 
=comment.user.username 

或者因爲你已經在這兩個MicropostComment

# Link: 
=link_to "User profile", (@micropost.user) 

# Name of the author 
[email protected] 
user_id說明
+0

由於某種原因'comment.user.username'會拋出錯誤:'nil:NilClass''undefined method'username' – alexchenco 2012-01-30 10:18:00