在我的控制器中,我有一个没有相应视图的动作。确切地说:上传图像的上传动作。但是,我需要当前用户ID来存储图像URL。但是current_user方法总是返回nil,因为action本身没有视图。在这种情况下,我如何获取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
答案 0 :(得分:1)
控制器方法实际上只是一个类方法。它不需要视图。我将它作为一个私有方法,该方法在类或从其继承的其他类之外是不可用的,因此它正确地不可用于视图。您的问题表明您的用户未登录或其他错误。你有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
此:
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相同。