TransWikia.com

Cancan wrong number of arguments (given 1, expected 0) con la acción new

Stack Overflow en español Asked by josedes on December 31, 2020

Estoy usando Cancancan y cuando llamo a la acción de index funciona bien, pero cuando intento llamar a new me muestra:

Completed 500 Internal Server Error in 10ms (ActiveRecord: 1.2ms)

ArgumentError – wrong number of arguments (given 1, expected 0):
actionpack (4.2.1) lib/action_controller/metal.rb:131:in initialize'
actionpack (4.2.1) lib/action_dispatch/routing/url_for.rb:104:in
initialize’ actionview (4.2.1) lib/action_view/layouts.rb:355:in
initialize' cancancan (2.0.0)
lib/cancan/controller_resource.rb:80:in
new’

No entiendo que sucede, alguien me puede ayudar

Este es mi applicationController.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.  

  protect_from_forgery with: :exception
  include CodigosGenerales
  before_action :cargarOpcionesDelPrincipal
  layout :colocar_layout
  helper_method :resource, :resource_name, :devise_mapping

  before_filter do
    resource = controller_path.singularize.gsub('/', '_').to_sym
    method = "#{resource}_params"
    params[resource] &&= send(method) if respond_to?(method, true)
  end

  rescue_from CanCan::AccessDenied do |exception|
      respond_to do |format|
        format.json { head :forbidden, content_type: 'text/html' }
        format.html { redirect_to root_path, alert: 'No tiene autorización para acceder' }
        format.js   { head :forbidden, content_type: 'text/html' }
    end
  end

  def current_ability
    controller_name_segments = params[:controller].split('/')
    controller_name_segments.pop
    controller_namespace = controller_name_segments.join('/').camelize
    Ability.new(current_usuario, controller_namespace)
    #Ability.new(current_usuario)
  end

 def resource_name
    :usuario
  end

  def resource
    @resource ||= Usuario.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:usuario]
  end

private

 def after_sign_out_path_for(resource_or_scope)
    new_usuario_session_path
end

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }

    devise_parameter_sanitizer.permit(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }

    devise_parameter_sanitizer.permit(:account_update) { 
                |u| u.permit(:nombre,
                             :apellido, 
                             :email,
                             :password, 
                             :password_confirmation, 
                             :current_password, 
                             :cedula,
                             :numero_excel,
                             :historia,
                             :estado,
                             :apellido, 
                             :telefono_habitacion, 
                             :telefono_trabajo,
                             :celular1, 
                             :celular2, 
                             :email2, 
                             :fecha_nacimiento, 
                             :ava,
                             :hong,
                             :berm,
                             :john,
                             :asp,
                             :blom,
                             :rol  ) }
  end
end

este es mi archivo model/ability.rb

class Ability
  include CanCan::Ability

  def initialize(user, controller_namespace)  
    user ||= Usuario.new # guest user (not logged in)
    can :manage, :welcome_con

    case controller_namespace
      when "Administrator"
        if user.rol == 1
            can :manage, Administrator::FaqsController
            can :manage, Administrator::TipsController
       end
        if user.rol == 2
            can :manage, :admin_vacunas
            can :manage, Administrator::ConfigurationsController
            can :manage, Administrator::ContactosController
            can :manage, Administrator::HomeTratamientoController
            can :manage, Administrator::VacunasController
        end

      when "paciente"
        if user.rol == 3
            can :manage, Paciente::HomeController
        end
      else  
            can :read, :all
     end 
  end
end

Y este es mi controlador:

class Administrator::FaqsController < ApplicationController 
include CodigosGenerales
 before_filter :authenticate_usuario!
  before_action :cargarOpcionesDelPrincipal
 load_and_authorize_resource :class => Administrator::FaqsController
 layout "administrator"

  def index
     @faqs = Faq.all.order('created_at desc')
     if @faqs.empty?
       flash.now[:warning] = "Aún no se han creado preguntas y respuestas" 
     end
  end

  def show
    @faq = Faq.find(params[:id])
  end

  def new
    @faq = Faq.new
  end

  def edit
    @faq = Faq.find( params[:id] )
  end

  def create
    @faq = Faq.new(faq_params)
    @faq.usuario_id = current_usuario.id
    respond_to do |format|
      if @faq.save
        format.html { redirect_to administrator_faq_path(@faq), notice: 'La pregunta fue creada exitosamente' }
        format.json { render :show, status: :created, location: @faq }
      else
        format.html { render :new }
        format.json { render json: @faq.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @faq = Faq.find params[:id]
    @faq.usuario_id = current_usuario.id
    respond_to do |format|
      if @faq.update(faq_params)
        format.html {redirect_to administrator_faq_path(@faq), notice: 'La pregunta fue modificada exitosamente' }
        format.json { render :show, status: :ok, location: @faq }
      else
        format.html { render :edit }
        format.json { render json: @faq.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @faq = Faq.find params[:id]
    @faq.destroy
    respond_to do |format|
      format.html { redirect_to administrator_faqs_path(@faq), notice: 'La pregunta fue eliminada exitosamente.' }
      format.json { head :no_content }
    end
  end

  private
    def set_faq
      @faq = Faq.find(params[:id])
    end

    def faq_params
      params.require(:faq).permit(:pregunta, :respuesta)
  end
end

STACK COMPLETO DEL ERROR:

ArgumentError – wrong number of arguments (given 1, expected 0):
actionpack (4.2.1) lib/action_controller/metal.rb:131:in initialize'
actionpack (4.2.1) lib/action_dispatch/routing/url_for.rb:104:in
initialize’
actionview (4.2.1) lib/action_view/layouts.rb:355:in initialize'
cancancan (2.0.0) lib/cancan/controller_resource.rb:80:in
new’
cancancan (2.0.0) lib/cancan/controller_resource.rb:80:in build_resource'
cancancan (2.0.0) lib/cancan/controller_resource.rb:61:in
load_resource_instance’
cancancan (2.0.0) lib/cancan/controller_resource.rb:35:in load_resource'
cancancan (2.0.0) lib/cancan/controller_resource.rb:28:in
load_and_authorize_resource’
cancancan (2.0.0) lib/cancan/controller_resource.rb:12:in block in add_before_action'
activesupport (4.2.1) lib/active_support/callbacks.rb:448:in
instance_exec’
activesupport (4.2.1) lib/active_support/callbacks.rb:448:in block in make_lambda'
activesupport (4.2.1) lib/active_support/callbacks.rb:164:in
block in halting’
activesupport (4.2.1) lib/active_support/callbacks.rb:504:in block in call'
activesupport (4.2.1) lib/active_support/callbacks.rb:504:in
each’
activesupport (4.2.1) lib/active_support/callbacks.rb:504:in call'
activesupport (4.2.1) lib/active_support/callbacks.rb:92:in
_run_callbacks’
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in _run_process_action_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in
run_callbacks’
actionpack (4.2.1) lib/abstract_controller/callbacks.rb:19:in process_action'
actionpack (4.2.1) lib/action_controller/metal/rescue.rb:29:in
process_action’
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:32:in block in process_action'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in
block in instrument’
activesupport (4.2.1) lib/active_support/notifications/instrumenter.rb:20:in instrument'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in
instrument’
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:30:in process_action'
actionpack (4.2.1) lib/action_controller/metal/params_wrapper.rb:250:in
process_action’
activerecord (4.2.1) lib/active_record/railties/controller_runtime.rb:18:in process_action'
actionpack (4.2.1) lib/abstract_controller/base.rb:137:in
process’
actionview (4.2.1) lib/action_view/rendering.rb:30:in process'
actionpack (4.2.1) lib/action_controller/metal.rb:196:in
dispatch’
actionpack (4.2.1) lib/action_controller/metal/rack_delegation.rb:13:in dispatch'
actionpack (4.2.1) lib/action_controller/metal.rb:237:in
block in action’
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in dispatch'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:43:in
serve’
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:43:in block in serve'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in
each’
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in serve'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:819:in
call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/rack/agent_hooks.rb:30:in
traced_call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/rack/browser_monitoring.rb:32:in
traced_call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/rack/developer_mode.rb:48:in
traced_call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
warden (1.2.6) lib/warden/manager.rb:35:in
block in call’
warden (1.2.6) lib/warden/manager.rb:34:in catch'
warden (1.2.6) lib/warden/manager.rb:34:in
call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
rack (1.6.4) lib/rack/etag.rb:24:in
call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in
call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
rack (1.6.4) lib/rack/head.rb:13:in
call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
actionpack (4.2.1) lib/action_dispatch/middleware/params_parser.rb:27:in
call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
actionpack (4.2.1) lib/action_dispatch/middleware/flash.rb:260:in
call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in
context’
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
actionpack (4.2.1) lib/action_dispatch/middleware/cookies.rb:560:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
activerecord (4.2.1) lib/active_record/query_cache.rb:36:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
activerecord (4.2.1) lib/active_record/migration.rb:378:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:29:in block in call'
activesupport (4.2.1) lib/active_support/callbacks.rb:88:in
_run_callbacks’
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in _run_call_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in
run_callbacks’
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
actionpack (4.2.1) lib/action_dispatch/middleware/reloader.rb:73:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
actionpack (4.2.1) lib/action_dispatch/middleware/remote_ip.rb:78:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in
better_errors_call’
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
web-console (2.3.0) lib/web_console/middleware.rb:28:in block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in
catch’
web-console (2.3.0) lib/web_console/middleware.rb:18:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
railties (4.2.1) lib/rails/rack/logger.rb:38:in call_app'
railties (4.2.1) lib/rails/rack/logger.rb:20:in
block in call’
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in block in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in
tagged’
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in tagged'
railties (4.2.1) lib/rails/rack/logger.rb:20:in
call’
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in call_with_quiet_assets'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
rack (1.6.4) lib/rack/methodoverride.rb:22:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
rack (1.6.4) lib/rack/runtime.rb:18:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
rack (1.6.4) lib/rack/lock.rb:17:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
rack (1.6.4) lib/rack/sendfile.rb:113:in call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in
call’
railties (4.2.1) lib/rails/engine.rb:518:in call'
railties (4.2.1) lib/rails/application.rb:164:in
call’
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in call'
rack (1.6.4) lib/rack/content_length.rb:15:in
call’
thin (1.7.0) lib/thin/connection.rb:86:in block in pre_process'
thin (1.7.0) lib/thin/connection.rb:84:in
catch’
thin (1.7.0) lib/thin/connection.rb:84:in pre_process'
thin (1.7.0) lib/thin/connection.rb:53:in
process’
thin (1.7.0) lib/thin/connection.rb:39:in receive_data'
eventmachine (1.2.0.1) lib/eventmachine.rb:194:in
run_machine’
eventmachine (1.2.0.1) lib/eventmachine.rb:194:in run'
thin (1.7.0) lib/thin/backends/base.rb:73:in
start’
thin (1.7.0) lib/thin/server.rb:162:in start'
rack (1.6.4) lib/rack/handler/thin.rb:19:in
run’
rack (1.6.4) lib/rack/server.rb:286:in start'
railties (4.2.1) lib/rails/commands/server.rb:80:in
start’
railties (4.2.1) lib/rails/commands/commands_tasks.rb:80:in block in server'
railties (4.2.1) lib/rails/commands/commands_tasks.rb:75:in
tap’
railties (4.2.1) lib/rails/commands/commands_tasks.rb:75:in server'
railties (4.2.1) lib/rails/commands/commands_tasks.rb:39:in
run_command!’
railties (4.2.1) lib/rails/commands.rb:17:in <top (required)>'
bin/rails:9:in
require’
bin/rails:9:in <top (required)>'
spring (1.7.2) lib/spring/client/rails.rb:28:in
load’
spring (1.7.2) lib/spring/client/rails.rb:28:in call'
spring (1.7.2) lib/spring/client/command.rb:7:in
call’
spring (1.7.2) lib/spring/client.rb:30:in run'
spring (1.7.2) bin/spring:49:in

spring (1.7.2) lib/spring/binstub.rb:11:in load'
spring (1.7.2) lib/spring/binstub.rb:11:in

bin/spring:13:in require'
bin/spring:13:in

bin/rails:3:in load'
bin/rails:3:in

One Answer

Según la documentación, la opción class es usada cuando el modelo a autorizar es distinto al nombre de tu controlador. En tu caso, como el modelo a usar en ese controlador es Faq, debería quedar como:

class Administrator::FaqsController < ApplicationController 
  load_and_authorize_resource :class => 'Faq'

Answered by Alter Lagos on December 31, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP