0

ブログの記事を編集するユーザーを取得しようとしています。記事を作成するユーザーを取得できますが、編集するユーザーを取得するときに行き詰まりました。<%= @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

エラー。

article_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(:バージョン => 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

記事を編集するユーザーを取得するのを手伝ってください。貼り付けるコードがさらに必要な場合は、お知らせください。

4

1 に答える 1