无法在ruby on rails中获得用户编辑文章



我正在尝试获得在博客中编辑文章的用户。我能够得到用户谁创建的文章,但卡住了,而得到用户谁编辑它。我试着用<%= @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

错误。

articles_controller.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>

schema.rb

ActiveRecord::Schema.define(:version => 20130421123420) 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"
    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

请帮我找到编辑文章的用户。如果你需要更多的代码来粘贴,请告诉我。

您正在ArticlesController#update中设置@article.user_id = current_user.id,但试图根据ArticlesController#show访问该值。这是行不通的。

因此,要在show.html.erb中看到@article.user_id的值,需要在相应的控制器ArticlesController#show中设置它,如下所示:
def show
  @article.user_id = current_user.id
end

如果它仍然不起作用,请发布你的模型关系,以及完整的错误信息&重现错误的步骤

最新更新