Displaying attributes for associated models in views

痴心易碎 提交于 2019-12-13 12:31:40

问题


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

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

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>

Articles table

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

I am able to fetch user id in views but no idea how to username or email. Any help would be appreciated.


回答1:


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.




回答2:


Already you set relation between the Article & User model. So in your article index page you have all articles in @articles variable. So easily you can get the particular user of a particular article using below code,

@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

like this you can get all the attribute of user.



来源:https://stackoverflow.com/questions/15862365/displaying-attributes-for-associated-models-in-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!