2013-04-07 15 views
1

我正在rails上編寫自己的博客。獲取在視圖上創建文章的用戶時,我被卡住了。基本上我想在我的博客應用程序中獲取創建文章(發佈)的用戶名。 我有user_id文章表中的用戶,文章表。我不知道如何獲取在應用程序中創建文章的用戶。基本上我想顯示在article/index.html頁面上創建文章的用戶的名稱(用戶名)。我試圖在文章控制器中創建文章時獲取用戶名如何獲得在博客上使用ruby在rails上創建文章的用戶

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

但無法獲得。

當前users_controller.rb爲空。

我是否需要在用戶和文章表之間進行連接,或者是否有可能無法連接到rails中。請建議我。

article/index.html。

<div class=""> 
    <title> <h2> learning experience blog </h2></title> 
</div> 

<div style="margin: 0 0 20px; padding: 20px 20px 10px; background: none repeat scroll 0 0 #82b548 ;"> 

<% @articles.each do |article| %> 
    <div style=" border-bottom: none:border: 1px solid #666666;border-radius: 1px 1px 1px 1px;margin: 0 0 20px; padding: 20px 20px 10px;height:280px; background: none repeat scroll 0 0 #FFFFFF "> 
    <div style="color:#51702E"><h2><%= article.title %></h2></div> 
    <div style="color:#666666; margin-top:10px"> <%= article.created_at %></div> 


    <hr style="border: 1px solid #FA9300;"> 
    <div style="margin-top:10px; color:"> <%= truncate(article.body, :length => 500, :separator => ' ') %></div> 
    <div style="margin-top:35px"> 
    <% if current_user.try(:is_admin) %> 
    <%= link_to "edit",edit_article_path(article), :class => 'btn btn-warning btn' %> 
    <%= link_to "delete",article_path(article),:method => :delete,:confirm => 'Are you sure?',:class => 'btn btn-danger' %> 
    <% end %> 
    <%= link_to "view more...",article_path(article), :class => 'btn btn-primary' %> 
    </div> 
    </div> 
    <% end %> 
<% if current_user.try(:is_admin) %> 
    <%= link_to "Create new Article", new_article_path, :class => 'btn btn-large btn-primary' %> 
    <% 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 

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 

schema.rb

# encoding: UTF-8 
# This file is auto-generated from the current state of the database. Instead 
# of editing this file, please use the migrations feature of Active Record to 
# incrementally modify your database, and then regenerate this schema definition. 
# 
# Note that this schema.rb definition is the authoritative source for your 
# database schema. If you need to create the application database on another 
# system, you should be using db:schema:load, not running all the migrations 
# from scratch. The latter is a flawed and unsustainable approach (the more migrations 
# you'll amass, the slower it'll run and the greater likelihood for issues). 
# 
# It's strongly recommended to check this file into your version control system. 

ActiveRecord::Schema.define(:version => 20130406120152) do 

    create_table "articles", :force => true do |t| 
    t.string "title" 
    t.text  "body" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    t.integer "user_id" 
    end 

    create_table "comments", :force => true do |t| 
    t.text  "content" 
    t.integer "user_id" 
    t.string "article_id" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

    create_table "users", :force => true do |t| 
    t.string "email",     :default => "", :null => false 
    t.string "encrypted_password",  :default => "", :null => false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   :default => 0 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.boolean "is_admin" 
    t.datetime "created_at",        :null => false 
    t.datetime "updated_at",        :null => false 
    t.string "name" 
    t.string "username" 
    t.boolean "is_active" 
    end 

    add_index "users", ["email"], :name => "index_users_on_email", :unique => true 
    add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true 

end 

請建議我如何獲取創建文章的用戶信息。

回答

-1

我相信你有你的模型中定義的關係。

class User 
    has_many :articles 
end 


class Article 
    belongs_to :user 
end 

現在,你可以簡單地做你的replyl

a = Article.find_by_id(1) 
a.user # to get the user for Article 
+0

喜Myth17,謝謝。但如何在articles_controller.rb和artilce/index.html.erb頁面中使用此信息。任何想法。 – 2013-04-07 08:16:35

+0

基本上我想獲取有關article/index.html.erb頁面上的文章的用戶信息。 – 2013-04-07 08:18:45

+0

嘗試:'@users = Article.all.map {| article | article.user}' – Zippie 2013-04-07 08:31:29

相關問題