2013-04-07 65 views
1

我想要在博客應用程序中創建文章的用戶的用戶名或電子郵件(都在用戶表中)。目前我能夠從articles_controller.rb獲取用戶ID在視圖中顯示相關模型的屬性

def create 
    @article = Article.new(params[:article]) 
    @article.user_id = current_user.id 
    @article.save 
    redirect_to article_path(@article) 
end 

但不知道如何獲取用戶名或電子郵件的相同。基本上我想在文章索引頁面上顯示用戶名或電子郵件。請建議我如何讓做了

user.rb

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

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

article.rb

class Article < ActiveRecord::Base 
    attr_accessible :title, :body 
    has_many :comments 
    belongs_to :user 
end 

articles_controller.rb

class ArticlesController < ApplicationController 
    def index 
     @articles = Article.all 
    end 

    def show 
     @article = Article.find(params[:id]) 
    end 

    def new 
     @article = Article.new 
    end 

    def create 
     @article = Article.new(params[:article]) 
     @article.user_id = current_user.id 
     @article.save 
     redirect_to article_path(@article) 
    end 

    def destroy 
     @article = Article.find(params[:id]) 
     @article.destroy 
     redirect_to action: 'index' 
    end 

    def edit 
     @article = Article.find(params[:id]) 
    end 

    def update 
     @article = Article.find(params[:id]) 
     @article.update_attributes(params[:article]) 
     flash.notice = "Article '#{@article.title}' Updated!" 
     redirect_to article_path(@article) 
    end 
end 

文/ index.html.erb

<div style="color:#666666; margin-top:10px"> <%= article.created_at %></div> 
    <div style="color:#666666; margin-top:10px"> <%= article.user_id %></div> 

文章表

class CreateArticles < ActiveRecord::Migration 
    def change 
    create_table :articles do |t| 
     t.string :title 
     t.text :body 
     t.timestamps 
    end 
    add_index :articles, [:user_id, :created_at] 
    end 
end 

我能夠在視圖中獲取用戶ID,但是不知道怎麼的用戶名或電子郵件。 任何幫助,將不勝感激。

回答

5

您已在Article模型類中定義user關聯belongs_to :user。這將創建在Article一個user方法返回相關的用戶,以便您在您的視圖:

<%= article.user.email %> 

將輸出相關的用戶的電子郵件,或:

<%= article.user.email if article.user %> 

迎合零用戶的值。或者寫一個幫手,將這種邏輯放在視圖之外。

+0

嗨,我在文章得到NoM​​ethodError#指數 顯示F:/24/billi/app/views/articles/index.html.erb其中第12行提出: 未定義的方法'電子郵件'爲零:NilClass錯誤。 – 2013-04-07 12:38:28

+0

嗨史蒂夫,我是否需要更改我的articles_controller.rb文件中的代碼。因爲在文章控制器中創建操作,我獲取當前用戶標識但沒有電子郵件。你有什麼建議? – 2013-04-07 12:40:53

+0

好吧,以便您的文章表中有一些沒有設置user_id值的記錄。您將需要防禦性地圍繞article.user返回nil。我更新了答案。 – Steve 2013-04-07 12:41:00

0

已經設置了文章&用戶模型之間的關係。因此,在您的文章索引頁中,您有@articles變量中的所有文章。所以,很容易就可以得到使用下面的代碼特定物品的特定用戶,

@articles.each do |article| 
    user = article.user #gives the user of this current article. From this 
         #objecct you can easily get the username, email or everything specific to user. 
    email = user.email #gives email of article's user or you can directly give artile.user.email 
end 

這樣就可以得到用戶的所有屬性。當試圖實現你的建議

+0

嗨Mohanrja,謝謝,我用<%= article.user.username if article.user%>得到了答案。 – 2013-04-07 14:13:03

相關問題