我正在尝试获取在博客中编辑文章的用户。我能够获得创建文章的用户,但在获得编辑它的用户时卡住了。我尝试<%= @aritcle_update_user.username unless @article.user.nil? %>
将其放入articles/show.html.erb
(基本上我试图在文章的显示页面上显示编辑用户)。我把这个
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
flash.notice = "Article '#{@article.title}' Updated!"
redirect_to article_path(@article)
@article.user_id = current_user.id
else
render 'edit'
end
end
在文章的更新动作中。在这里,我尝试@article.user_id = current_user.id
使用它并在文章的显示页面中使用它,但它抛出了我
NoMethodError in Articles#show
Showing f:/kuta/billi/app/views/articles/show.html.erb where line #36 raised:
undefined method `username' for 2:Fixnum
错误。
文章控制器.rb
class ArticlesController < ApplicationController
before_filter :is_user_admin, only: [:new, :create, :edit, :destroy]
before_filter :log_impression, :only=> [:show]
def is_user_admin
redirect_to(action: :index) unless current_user.try(:is_admin?)
return false
end
def log_impression
@article = Article.find(params[:id])
# this assumes you have a current_user method in your authentication system
@article.impressions.create(ip_address: request.remote_ip,user_id:current_user.id)
end
def index
@articles = Article.all(:order => "created_at DESC")
@article_titles = Article.first(10)
@tags = Tag.all
end
def show
@article = Article.find(params[:id])
@related_articles = Article.joins(:taggings).where('articles.id != ?', @article.id).where(taggings: { tag_id: @article.tag_ids })
@article_popular = Article.order('articles.impressions_count DESC').limit(5)
end
def new
@article = Article.new
end
def create
@article = Article.new(params[:article])
@article.user_id = current_user.id
if @article.save
flash[:success] = "article created!"
redirect_to article_path(@article)
else
render 'new'
end
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])
if @article.update_attributes(params[:article])
flash.notice = "Article '#{@article.title}' Updated!"
redirect_to article_path(@article)
else
render 'edit'
end
end
end
文章/show.html.erb
<div style="margin-top:20px;margin-left:-10px""> <li> edit by<%= @aritcle_update_user.username unless @article.user.nil? %> </div>
架构.rb
ActiveRecord::Schema.define(:version => 20130421123420) 做
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"
t.integer "impressions_count"
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 "impressions", :force => true do |t|
t.string "impressionable_type"
t.integer "impressionable_id"
t.integer "user_id"
t.string "ip_address"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "article_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "taggings", ["article_id"], :name => "index_taggings_on_article_id"
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
create_table "tags", :force => true do |t|
t.string "name"
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.boolean "is_active"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "username"
end
请帮助我获取编辑文章的用户。如果您需要粘贴更多代码,请告诉我。