我有一个ruby on rails应用程序,我使用cancancan gem定义了我的用户权限,它过去可以正常工作,最近我在应用程序中添加了国际化功能,此后load_and_authorize_resource
命令导致了{{1 }},每当我尝试在应用程序中创建任何新对象时。如果我注释掉该行,它会很好,或者即使我只保留argument error,When assigning attributes, you must pass a hash as an argument.
也可以。我无法在应用程序控制器中跳过authorize_resource
,因为这样会使我在依赖于该应用程序的其他模型中加载错误。我尝试在这些模型上分别添加load_resources
或:before_filter
,但这也不起作用。谁能帮助我解决该错误的方法。
这是我的代码的一部分。
load_resources
capability.rb文件:
My application controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
around_action :set_current_user, unless: :devise_controller?
before_action :configure_permitted_parameters, if: :devise_controller?
devise_group :user, contains: [:employee, :admin]
before_action :authenticate_user!
before_action :tickets_data, if: :tickets_and_dashboard_controller?
load_and_authorize_resource unless: :devise_controller?
before_action :set_raven_context
after_action :allow_shopify_iframe
before_action :set_locale
include Current
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:account_update, keys: [:first_name,:last_name,:password,:email, :username,:street1, :street2, :state, :zipcode, :city,:phone, :gender, :status,:birthday,:employee_type])
devise_parameter_sanitizer.permit(:sign_up, keys: [:employment_type, :work_group_id, :email, :password, :password_confirmation, :first_name, :last_name, :street1, :street2, :city, :state, :zipcode, :phone, :avatar, :birthday, :gender, :status, :department_id, :work_class_id
])
end
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url
flash[:notice] = exception
end
def default_url_options
{ locale: (current_user.language || Setting.first.language rescue "en")}
end
private
def set_locale
I18n.locale = (current_user.language || Setting.first.language || I18n.default_locale) rescue "en"
end
def allow_shopify_iframe
response.headers['X-Frame-Options'] = 'SAMEORIGIN'
end
def tickets_and_dashboard_controller?
request.path.eql?(tickets_path) || request.path.eql?(root_path)
end
def tickets_data
@open_tickets_count = Ticket.open.count rescue 0
@guest_tickets_count = Ticket.guest_tickets.count rescue 0
@maintenance_tickets_count = Ticket.maintenance_tickets.count rescue 0
@my_tickets = Ticket.where("assignee_id = ?" , current_user.id).order("id desc")
end
def set_current_user
Current.user = current_user
yield
ensure
Current.user = nil
end
def set_raven_context
Raven.user_context(id: session[:current_user_id])
Raven.extra_context(params: params.to_unsafe_h, url: request.url)
end
end
预先感谢