【问题标题】:Displaying attributes for associated models in views在视图中显示关联模型的属性
【发布时间】:2013-04-07 12:27:10
【问题描述】:

我想获取在博客应用程序中创建文章的用户的用户名或电子邮件(两者都在用户表中)。目前我可以从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

但不知道如何获取相同的用户名或电子邮件。基本上我想在文章索引页面上显示用户名或电子邮件。请建议我如何完成它

用户.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,但不知道如何输入用户名或电子邮件。 任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby associations rails-activerecord


    【解决方案1】:

    您已在 Article 模型类上与 belongs_to :user 定义了 user 关联。这会在 Article 上创建一个 user 方法,该方法返回关联的用户,以便您在视图中看到:

    <%= article.user.email %>
    

    将输出关联用户的电子邮件,或者:

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

    满足 nil 用户值。或者编写一个帮助程序将这个逻辑排除在视图之外。

    【讨论】:

    • 嗨,当尝试实施您的建议时,我在 Articles#index Showing F:/24/billi/app/views/articles/index.html.erb 中遇到 NoMethodError 其中第 12 行提出:未定义的方法`email' 用于 nil:NilClass 错误。
    • 嗨史蒂夫,我需要更改我的articles_controller.rb 文件中的代码吗?因为在文章控制器中的创建操作中,我得到了当前用户 ID,但没有电子邮件。你有什么建议?
    • 好的,所以您的文章表中有一些没有设置 user_id 值的记录。您将需要围绕 article.user 返回 nil 进行防御性编码。我更新了答案。
    • 第二条评论:不,您不需要在控制器中再做任何事情。您正在设置 user_id,这足以将文章与该用户相关联。
    • 如果 user_id 为 nil,则 article.user 返回 nil。 nil 只是 Ruby 中的一个对象,但它没有 email 方法(这就是你得到错误的原因)。添加行尾 if 将确保该行仅在 if 评估为 true 时执行。因此,要处理您的错误 if article.user 会阻止您在 nil 上调用 email 方法。
    【解决方案2】:

    您已经设置了文章和用户模型之间的关系。因此,在您的文章索引页面中,您拥有 @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
    

    这样就可以获取用户的所有属性了。

    【讨论】:

    • 嗨 Mohanrja,谢谢,我使用 得到了答案。
    猜你喜欢
    • 2013-03-05
    • 2013-07-29
    • 2016-04-14
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多