设计/导轨 - 未定义的方法`许可'

时间:2015-06-21 04:08:32

标签: ruby-on-rails ruby ruby-on-rails-4 devise

问题:我正在尝试创建一个自定义操作,允许教师通过"批准"批准学生。行动。批准操作将获取相应的行并从" pending"更改状态。到"活跃"并保存记录。目前,当我点击链接时,更好的错误会给我一个

"未定义方法`permit' for" 2":String"

它提到了一些关于

的事情

设计:: ParameterSanitizer#许可证

请记住,我对Devise很新,我很难过!

控制器

class Teachers::RegistrationsController < Devise::RegistrationsController

    # APPROVE
      def approve
        @ts = TeacherStudent.find_by(teacher_id: params[:teacher], student_id: params[:student])
        @ts.status = "active"
        @ts.save
        redirect_to teachers_path
      end

end

查看

= link_to "Approve", approve_path(teacher: s.teacher_id, student: s.student_id), method: :put

的routes.rb

put 'teachers' => 'teachers#approve', as: :approve

错误

Started PUT "/teachers?student=14&teacher=2" for ::1 at 2015-06-21 01:06:33 -0500
Processing by Teachers::RegistrationsController#update as HTML
  Parameters: {"authenticity_token"=>"VQ2zVIv5gof1+38xdAA9d/Y+bWmHk4bT1SXHvc+xLr3+qTOm2xSMZUFjDw74umk35P36kgpqx4eMftFGyVThSg==", "student"=>"14", "teacher"=>"2"}
  [1m[36mTeacher Load (0.6ms)[0m  [1mSELECT  "teachers".* FROM "teachers" WHERE "teachers"."id" = $1  ORDER BY "teachers"."id" ASC LIMIT 1[0m  [["id", 2]]
  [1m[35mTeacher Load (0.3ms)[0m  SELECT  "teachers".* FROM "teachers" WHERE "teachers"."id" = $1 LIMIT 1  [["id", 2]]
Completed 500 Internal Server Error in 5ms (ActiveRecord: 1.0ms)

NoMethodError - undefined method `permit' for "2":String:
   () Users/dannysun/.rvm/gems/ruby-2.2.1/bundler/gems/devise-15b99977a2a3/lib/devise/parameter_sanitizer.rb:66:in `permit'
   () Users/dannysun/.rvm/gems/ruby-2.2.1/bundler/gems/devise-15b99977a2a3/lib/devise/parameter_sanitizer.rb:58:in `account_update'
   () Users/dannysun/.rvm/gems/ruby-2.2.1/bundler/gems/devise-15b99977a2a3/lib/devise/parameter_sanitizer.rb:77:in `default_sanitize'
   () Users/dannysun/.rvm/gems/ruby-2.2.1/bundler/gems/devise-15b99977a2a3/lib/devise/parameter_sanitizer.rb:24:in `sanitize'
   () Users/dannysun/.rvm/gems/ruby-2.2.1/bundler/gems/devise-15b99977a2a3/app/controllers/devise/registrations_controller.rb:141:in `account_update_params'
   () Users/dannysun/.rvm/gems/ruby-2.2.1/bundler/gems/devise-15b99977a2a3/app/controllers/devise/registrations_controller.rb:48:in `update'
  actionpack (4.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.1) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.1) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
  activesupport (4.2.1) lib/active_support/callbacks.rb:505: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/routing/mapper.rb:49: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 `serve'
  actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:819:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/flash.rb:260: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'
  actionpack (4.2.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.1) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in `call'
  activerecord (4.2.1) lib/active_record/migration.rb:378: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'
  actionpack (4.2.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/remote_ip.rb:78: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'
  actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  web-console (2.1.3) lib/web_console/middleware.rb:37:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30: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'
  actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.1) lib/rails/engine.rb:518:in `call'
  railties (4.2.1) lib/rails/application.rb:164:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /Users/dannysun/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /Users/dannysun/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /Users/dannysun/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

2 个答案:

答案 0 :(得分:0)

问题很可能出在您的权限中。

您使用的是强参数吗?如果是这样,您需要允许那里的字段。

示例:

def teacher_params
  params.require(:teacher).permit(:email, :current_password, :password, :password_confirmation)
end

答案 1 :(得分:0)

在教师控制器底部添加以下代码 -

private
def user_params
  accessible = [:email, _____] # extend with your own params
  accessible << [ :password, :password_confirmation ] unless params[:teacher][:password].blank?
  params.require(:teacher).permit(accessible)
end