代码之家  ›  专栏  ›  技术社区  ›  Shripad Krishna

获取当前用户是否需要相应的视图?

  •  0
  • Shripad Krishna  · 技术社区  · 14 年前

    在我的控制器中,我有一个没有相应视图的动作。准确地说:上传图片的上传动作。但是,我需要当前用户id来存储图像url。但是当前的用户方法总是返回nil,因为操作本身没有视图。在这种情况下,如何获取当前用户?我正在使用authlogic。我的应用程序\u controller.rb包含以下内容:

    class ApplicationController < ActionController::Base
    
      helper :all
      helper_method :current_user_session, :current_user
      filter_parameter_logging :password, :password_confirmation
      protect_from_forgery
    
      def correct_safari_and_ie_accept_headers
        ajax_request_types = [ 'application/json', 'text/javascript', 'text/xml']
        request.accepts.sort!{ |x, y| ajax_request_types.include?(y.to_s) ? 1 : -1 } if request.xhr?
      end
    
      private
        def set_cache_buster
           response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
           response.headers["Pragma"] = "no-cache"
           response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
        end
    
        def current_user_session
          return @current_user_session if defined?(@current_user_session)
          @current_user_session = UserSession.find
        end
    
        def current_user
          return @current_user if defined?(@current_user)
          @current_user = current_user_session && current_user_session.record
        end
    end
    

    current_user 助手方法。只有 upload 行动是不可能的。代码:

    控制器:

    class ImageStacksController < ApplicationController
     def upload
        # Using swfupload.
    
        image_stack_params = { 
          :caption => params[:caption], 
          :swf_uploaded_data => params[:link]
          }
    
        # current_user returns nil, even with user logged in!!
        # Occurs only in the upload action and no other action in this controller.
        logger.info("Current User: #{current_user}") #Returns nil
        @image_stack = current_user.image_stacks.create! image_stack_params
    
    
          respond_to do |format|
            format.js { render :json => @image_stack }
          end
      end
    
      def edit
       logger.info("Current User: #{current_user}") #Returns current user
      end
    
      def new
       logger.info("Current User: #{current_user}") #Returns current user
      end
    
      def update
       logger.info("Current User: #{current_user}") #Returns current user
      end
    
      def destroy
       logger.info("Current User: #{current_user}") #Returns current user
      end
     end
    

    型号:

    class ImageStack < ActiveRecord::Base
      belongs_to :user, :class_name => "User", :foreign_key => "user_id"
    
      upload_image_to_s3 :link
    
      def swf_uploaded_data=(data)
          data.content_type = MIME::Types.type_for(data.original_filename)
          self.link = data
      end  
    end
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   mark    14 年前

    控制器方法实际上就是,类方法。它不需要视图。我使它成为一个私有方法这个方法在类或从它继承的其他类之外是不可用的,因此它对视图是不可用的。您的问题表明您的用户未登录或其他错误。您是否有require\u user方法?

    #application_controller
    private
    
    def require_user
      unless current_user
        store_location
        flash[:notice] = t(:must_be_logged_in)
        redirect_to user_login_path
        return false
      end
    end
    
    def store_location
      session[:return_to] = request.request_uri
    end
    
    #user.rb
    has_many :images
    
    #image.rb
    belongs_to :user    
    
    # image_controller.rb
    before_filter :require_user
    
    def create
      @photo = @item.images.new(:photo => params[:photo],  :user => current_user)
    

    您当前的用户方法是已继承的ApplicationController方法:

    ImageStacksController < ApplicationController
    

    这是:

    helper_method :current_user_session, :current_user
    

    upload操作和其他操作的区别在于update是由javascript调用的。我记得做了一个类似的上传和必须通过的真实性令牌。日志中还有什么其他记录吗?

    这可能对您有用: http://github.com/JimNeath/swfupload---paperclip-example-app

    - content_for :head do
      = javascript_tag "var AUTH_TOKEN = #{form_authenticity_token.inspect};" if protect_against_forgery?
    

    现在,您将字段添加到swflupload,方法与添加当前用户的方法相同。