Ruby on rails 在视图中显示关联模型的属性
我想获取在博客应用程序中创建文章的用户的用户名或电子邮件(两者都在用户表中)。目前我可以从articles_controller.rb获取用户idRuby on rails 在视图中显示关联模型的属性,ruby-on-rails,ruby,associations,rails-activerecord,Ruby On Rails,Ruby,Associations,Rails Activerecord,我想获取在博客应用程序中创建文章的用户的用户名或电子邮件(两者都在用户表中)。目前我可以从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 class ArticlesController &l
def create
@article = Article.new(params[:article])
@article.user_id = current_user.id
@article.save
redirect_to article_path(@article)
end
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
但不知道如何获取相同的用户名或电子邮件。基本上我想在文章索引页面上显示用户名或电子邮件。请告诉我怎么做
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
class用户
第1.rb条
class Article < ActiveRecord::Base
attr_accessible :title, :body
has_many :comments
belongs_to :user
end
类文章
文章\u controller.rb
def create
@article = Article.new(params[:article])
@article.user_id = current_user.id
@article.save
redirect_to article_path(@article)
end
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
class-ArticlesController
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
classcreatearticles
我能够在视图中获取用户id,但不知道如何使用用户名或电子邮件。
如果有任何帮助,我们将不胜感激。您已经在您的
文章中定义了用户
与所属的模型类的关联。\u to:user
。这将在文章
上创建一个用户
方法,该方法返回关联的用户,以便您在视图中:
<%= article.user.email %>
将输出关联用户的电子邮件,或:
<%= article.user.email if article.user %>
以满足零用户价值。或者编写一个助手,将此逻辑排除在视图之外。您已经设置了文章和用户模型之间的关系。因此,在文章索引页面中,@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
这样,您就可以获得用户的所有属性。hi当尝试实现您的建议时,我在文章中得到了NoMethodError,索引显示F:/24/billi/app/views/Articles/index.html.erb,其中第12行提出了:未定义的方法“email”表示nil:NilClass错误。hi Steve,我需要更改我的文章中的代码吗。因为在文章控制器中的创建操作中,我得到的是当前用户id,但没有电子邮件。你有什么建议?好的,那么你的articles表中有一些记录没有设置user\u id值。您需要对article.user返回nil进行防御性编码。我更新了答案。请重新评论:不,您不需要在控制器中执行任何操作。您正在设置用户id,这足以将文章与该用户关联。如果user\u id
为nil,则article.user返回nil。nil只是Ruby中的一个对象,但它没有email
方法(这就是为什么会出现错误)。如果在if
中添加一个行尾,将确保仅当if的计算结果为true时才执行该行。因此,为了处理您的错误,if article.user
阻止您在nil上调用email
方法。你好,Mohanrja,谢谢,我使用获得了答案。