Требует ли выборка current_user соответствующего представления? - PullRequest
0 голосов
/ 02 августа 2010

В моем контроллере у меня есть действие, которое не имеет соответствующего представления.Точно: действие загрузки для загрузки изображений.Тем не менее, мне требуется идентификатор текущего пользователя для хранения URL изображения.Но метод current_user всегда возвращает nil, поскольку само действие не имеет представления.В таких случаях, как я могу получить current_user?Я использую authlogic.Мой application_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 Ответ

1 голос
/ 02 августа 2010

Метод контроллера - это на самом деле метод класса.Это не требует представления.Я сделал его закрытым методом, метод недоступен за пределами класса или других классов, унаследованных от него, и поэтому он правильно недоступен для представления.Ваша проблема предполагает, что ваш пользователь не вошел в систему или что-то еще не так.У вас есть метод require_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)

Редактировать:

Ваш метод current_user - это метод ApplicationController, который уже унаследован:

ImageStacksController < ApplicationController

This:

helper_method :current_user_session, :current_user

предоставляет методы для представления.

Разница между действием загрузки и всеми остальными обновлениями вызывается с помощью javascript.Я помню, как делал похожий загрузчик и должен был передать маркер подлинности.О чем-либо еще сообщается в журнале?

Это может быть полезно для вас: http://github.com/JimNeath/swfupload---paperclip-example-app

Предоставление токену подлинности, доступному для js, выглядит примерно так:

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

Теперь вы добавляете поле для swflupload так же, как вы добавили current_user.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...