1

我正在用 ruby​​ on rails 写自己的博客。我在获取在视图上创建文章的用户时遇到了困难。基本上我想获取在我的博客应用程序中创建文章(帖子)的用户的用户名。我有用户,文章表,我在文章表中有 user_id。我不知道如何获取在应用程序中创建文章的用户。基本上我想在 article/index.html 页面上显示创建文章的用户的名称(用户名)。我试图在文章控制器中创建文章时获取用户名

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

但无法得到。

目前 users_controller.rb 是空的。

我是否需要在用户和文章表之间加入,或者是否可以不加入导轨。请给我建议。

文章/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 %>

文章控制器.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

文章_rb

class Article < ActiveRecord::Base
   attr_accessible :title, :body
   has_many :comments
   belongs_to :user
end

架构.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

请建议我如何获取创建文章的用户信息。

4

1 回答 1

-1

我相信你的模型中定义了关系。

class User
   has_many :articles
end


class Article
  belongs_to :user
end

现在你可以简单地做

a = Article.find_by_id(1)
a.user # to get the user for Article
于 2013-04-07T07:46:26.827 回答