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 %>
article_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
記事を作成したユーザー情報を取得する方法を教えてください。