显示属性中的观点相关车型 [英] Displaying attributes for associated models in views
问题描述
我要获取用户的用户名或电子邮件(两者都在用户表)谁在创建博客应用的文章。目前,我能够从articles_controller.rb获取用户ID
I want to fetch username or email( both are in user table) of user who creates article in blog application. Currently I am able to fetch user id from articles_controller.rb
def create
@article = Article.new(params[:article])
@article.user_id = current_user.id
@article.save
redirect_to article_path(@article)
end
但不知道如何为获取相同的用户名或电子邮件。基本上我想显示文章索引页面上的用户名或电子邮件。请建议我如何让做了
but no idea how to fetch username or email for same. Basically I want to display username or email on article index page. Please suggest me to how to get done it
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
article.rb
class Article < ActiveRecord::Base
attr_accessible :title, :body
has_many :comments
belongs_to :user
end
articles_controller.rb
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
article/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,但是不知道怎么的用户名或电子邮件。
任何帮助将是AP preciated。
I am able to fetch user id in views but no idea how to username or email. Any help would be appreciated.
推荐答案
您已经定义了一个用户
在文章协会
与模型类belongs_to的:用户
。这将创建对文章
返回相关的用户,以便您在视图中的用户
方法:
You have defined a user
association on your Article
model class with belongs_to :user
. This creates a user
method on Article
that returns the associated user so you in your view:
<%= article.user.email %>
将输出相关的用户的电子邮件,或者:
will output the email of the associated user, or:
<%= article.user.email if article.user %>
,以满足用户的零值。或者写一个帮手因素这个逻辑了看法。
to cater for nil user values. Alternatively write a helper to factor this logic out of the view.
这篇关于显示属性中的观点相关车型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!