0

ブログで記事を編集するユーザーを取得中に行き詰まりました。次のような記事を作成するユーザーを取得できます:-<%= @article.user.username if @article.user %>記事を編集する機能を既に実装していますが、記事を編集するユーザーを取得する方法がわかりません。

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 class="row-fluid">
      <div class="span12">
      <div class ="span9">
        <div class ="row-fluid">

          <div id="upperborder">
              <div id="lowerborder">
                <div style="color:#1fb2e8;font-size:2.3em;"><%= @article.title %></div>
                 <div id="tabs"> 
                  <ul id="tabs">
                  <li>  <%= time_tag(@article.created_at.in_time_zone("Asia/Calcutta")) %> </li>
                  <li>   <%= @article.user.username if @article.user %></li>
                   <li>
                     <% unless @article.comments.empty? %>
                     (<%= @article.comments.size %>) comments
                     <% end %>
                     </li>
                     <li>
                     <% @article.tags.each do |tag| %>
                      <%= link_to tag.name, tag_path(tag) %>
                    <% end %>     
                  </li>
                 </ul>
               </div>            

             <hr id="upperline">
             <div style="font-size: 1.2em"><%= @article.body %></div>

           <div id="tabs"> 
              <ul id="tabs">
                <div style="margin-top:20px; margin-left:10px"> <li> <%= link_to "back",articles_path, :class => ''  %></li></div>
                 <% if current_user.try (:is_admin) %>
                  <div style="margin-top:20px;margin-left:-10px""> <li> <%= link_to "edit", edit_article_path(@article), :class => '' %></li> </div>
                  <div style="margin-top:20px"> <li> <%= link_to "delete",article_path(@article),:method => :delete,:confirm => 'Are you sure?',:class => '' %></li> </div>
                <% end %>
              </ul>
            </div>       

           <div id="fabs">
             <ul id="fabula">
               <div style="margin-left:250px; font-size:1.2em;color:#1fb2e8;margin-top:-0px">  Viewed: <%=@article.impression_count %> times, 
               <% if (@article.created_at != @article.updated_at) %>
               edit: <%= time_ago_in_words(@article.updated_at) %> ago,
               <% end %>
               <% if ! @article.comments.empty? %>
               Active: <%=  time_ago_in_words(@article.comments.last.created_at) %> ago
               <% end %>
               </div>
             </ul>
           </div>

           <hr style="border: 1px solid #E0E0F0; margin-top:50px">
           <div id="commentform">
             <%= render :partial => 'comments/comment_form' %>
               <% @article.comments.each do |c| %>
                 <% if !c.nil? %>
                   <div id ="commentdisplay"> <%= render partial: 'comments/comment', :locals => { :comment => c } %> </div>
                 <% end %> 
                <% end %> 
           </div> 
          </div>
         </div>
       </div>

           <div class="span3">    
             <div id="popular"> 
              <% if !@article_popular.empty? %>
                <div id="headerpopular"> most viewed articles </div>
                 <% @article_popular.each do |article_popular| %>
                  <div id="popular-title">  <%= link_to  truncate(article_popular.title, :length => 35, :separator => ' '),  article_path(article_popular) %></div>
                 <% end %>
              <% end %>
            </div>
         </div>
       </div>

           <% if !@related_articles.empty? %>
              <div id = "related-border">
              <div id="related-title"> Related articles </div>
              <% @related_articles.each do | related_article | %>       
              <div id="related-title-link">  <%= link_to  truncate(related_article.title, :length => 35, :separator => ' '),  article_path(related_article) %></div>
            <% end %>
            </div> 
            <% else %>
              <div id="related-border">
              <div id="ygm"> You got us ! </div>
              <div id="nofound"><h3> no related article found :(</h3></div>
              </div> 
            <% end %>

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

記事.rb

class Article < ActiveRecord::Base
   attr_accessible :title, :body
   attr_accessible :tag_list
   has_many :comments
   belongs_to :user
   has_many :taggings, :dependent => :destroy
   has_many :tags, through: :taggings , :dependent => :destroy
    has_many :impressions, as: :impressionable 
   validates :title, :body, :tag_list,  :presence => true

  def impression_count
    impressions.size
  end

  def unique_impression_count
    impressions.group(:ip_address).size #UNTESTED: might not be correct syntax
  end

   def tag_list
     self.tags.collect do |tag|
      tag.name
     end.join(", ")
   end

   def tag_list=(tags_string)
     tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq
     new_or_found_tags = tag_names.collect { |name| Tag.find_or_create_by_name(name) }
     self.tags = new_or_found_tags
   end
end

user.rb

class User < ActiveRecord::Base
    has_many :articles, :order => "created_at DESC"
    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

記事を編集するユーザーを取得する必要があります。ここに貼り付けるコードがさらに必要な場合はお知らせください。ありがとう。

4

2 に答える 2

0

コントローラーのアクションでメソッドを使用して、current_user編集中のユーザーを取得します。

于 2013-04-27T17:26:32.720 に答える