无法在rails上的ruby中获取用户编辑文章

时间:2013-04-27 20:23:29

标签: ruby-on-rails ruby blogs

我正在尝试让用户在博客中编辑文章。我可以让用户创建文章,但在获得编辑的用户时卡住了。我尝试将<%= @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 =&gt; 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

请帮助我获取编辑文章的用户。如果您需要更多代码粘贴,请告知我们。

1 个答案:

答案 0 :(得分:0)

您在@article.user_id = current_user.id中设置了ArticlesController#update,但尝试根据ArticlesController#show访问该值。那样不行。

所以,要查看@article.user_id中的值show.html.erb,您需要在相应的控制器ArticlesController#show中设置它,如下所示:

def show
  @article.user_id = current_user.id
end

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