cancan은 대표님과 함께 NoMethodError로 연결됩니다.

9351 단어 cancan

묘사

안녕하세요. CanCan을 사용하다가 문제가 생겼습니다.나는 능력 과목이 하나 있는데, 내용은 다음과 같다.
class Ability
  include CanCan::Ability

  def initialize(user)
    user||=User.new
    if user.admin?
      can :manage, :all
    else
      can :create, :all 
      can :new, :all 
      can :manage, :all,:user_id=>user.id
    end 
  end
end
저 하나 더 있어요.
   delegate :user_id, :to=>:map, :allow_nil=> true
모델의 새 인스턴스를 생성하는 동안 오류가 발생했습니다.
#Plot: 0x007fc7b0bc5910의 정의되지 않은 메서드 "user id="
틀림없이 음모 때문일 것이다.매핑이 아직 인스턴스화되지 않았습니다.
하지만, rails 컨트롤러를 불러와서 실행하면
  plot=Plot.new
  plot.user_id
음모user id가 nil로 반환되고 예외가 발생하지 않습니다.
내가 잘못한 것이 있습니까? 아니면 이것이 진정한 문제입니까?

토론 #1

디버깅 절차로 can :manage, :all,:user_id=>user.id 클래스에서 Ability 줄을 삭제하면 '정의되지 않은 방법' 이 사라집니까?
#Plot의 "undefined method'user id="을 받았을 때, 창고 추적에 cancan이 포함되어 있습니까?
documentation에 따르면 delegate에 대한 호출은 문법적으로 정확해 보입니다.생산 코드Plot의 실례화와 rails 컨트롤러의 실례화 사이에 약간의 차이가 있을 수 있습니까?
파일에서 "방법은 실례 변수, 클래스 변수, 상수... 에 위탁할 수 있다."라고 말했다.:map 어느 것을 말합니까?

토론 #2

Jared 만약 내가 그 줄을 삭제한다면, 그렇습니다. 오류는 사라질 것입니다.
창고 추적은 확실히 cancan과 관련이 있습니다. (다음 글 참조)
:map 기호는 실례 변수를 표시합니다. (정의되지 않으면 nil)
스택 추적
ruby/1.9.1/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:407:in `method_missing'
ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:149:in `method_missing'
ruby/1.9.1/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:92:in `block in assign_attributes'
ruby/1.9.1/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:91:in `each'
ruby/1.9.1/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:91:in `assign_attributes'
ruby/1.9.1/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:86:in `build_resource'
ruby/1.9.1/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:66:in `load_resource_instance'
ruby/1.9.1/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:32:in `load_resource'
ruby/1.9.1/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
ruby/1.9.1/gems/cancan-1.6.10/lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:418:in `_run__503387068778273814__process_action__2741409849370816285__callbacks'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
ruby/1.9.1/gems/actionpack-3.2.8/lib/abstract_controller/callbacks.rb:17:in `process_action'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_controller/metal/rescue.rb:29:in `process_action'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/notifications.rb:123:in `block in instrument'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/notifications.rb:123:in `instrument'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
ruby/1.9.1/gems/actionpack-3.2.8/lib/abstract_controller/base.rb:121:in `process'
ruby/1.9.1/gems/actionpack-3.2.8/lib/abstract_controller/rendering.rb:45:in `process'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_controller/metal.rb:203:in `dispatch'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_controller/metal.rb:246:in `block in action'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:73:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:36:in `call'
ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:68:in `block in call'
ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:56:in `each'
ruby/1.9.1/gems/journey-1.0.4/lib/journey/router.rb:56:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:600:in `call'
ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:35:in `block in call'
ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:34:in `catch'
ruby/1.9.1/gems/warden-1.2.1/lib/warden/manager.rb:34:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
ruby/1.9.1/gems/rack-1.4.4/lib/rack/etag.rb:23:in `call'
ruby/1.9.1/gems/rack-1.4.4/lib/rack/conditionalget.rb:25:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/head.rb:14:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/flash.rb:242:in `call'
ruby/1.9.1/gems/rack-1.4.4/lib/rack/session/abstract/id.rb:210:in `context'
ruby/1.9.1/gems/rack-1.4.4/lib/rack/session/abstract/id.rb:205:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/cookies.rb:339:in `call'
ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/query_cache.rb:64:in `call'
ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `_run__1190187055330524715__call__110718261726782402__callbacks'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/reloader.rb:65:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
ruby/1.9.1/gems/railties-3.2.8/lib/rails/rack/logger.rb:26:in `call_app'
ruby/1.9.1/gems/railties-3.2.8/lib/rails/rack/logger.rb:16:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/request_id.rb:22:in `call'
ruby/1.9.1/gems/rack-1.4.4/lib/rack/methodoverride.rb:21:in `call'
ruby/1.9.1/gems/rack-1.4.4/lib/rack/runtime.rb:17:in `call'
ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
ruby/1.9.1/gems/rack-1.4.4/lib/rack/lock.rb:15:in `call'
ruby/1.9.1/gems/actionpack-3.2.8/lib/action_dispatch/middleware/static.rb:62:in `call'
ruby/1.9.1/gems/railties-3.2.8/lib/rails/engine.rb:479:in `call'
ruby/1.9.1/gems/railties-3.2.8/lib/rails/application.rb:223:in `call'
ruby/1.9.1/gems/rack-1.4.4/lib/rack/content_length.rb:14:in `call'
ruby/1.9.1/gems/railties-3.2.8/lib/rails/rack/log_tailer.rb:17:in `call'
ruby/1.9.1/gems/rack-1.4.4/lib/rack/handler/webrick.rb:59:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

토론 #셋

는 자신의 능력 범위 내에서 조건 산열을 사용했기 때문에 cancan 가설user_id은 데이터베이스 열입니다.이것은 비키의 Defining Abilities에 기록되어 있다.

It is important to only use database columns for these conditions so it can be used for Fetching Records.


해봐도 돼Defining Abilities with Blocks

토론 #4

아, 알았어.도와 주셔서 감사합니다.블록으로 그것을 정의하면 확실히 예상대로 일할 수 있다.그러나 이러한 능력이 산열로 정의되고 객체를 편집, 보기 또는 삭제할 때 위임 속성이 관련될 때 작업이 성공적(위임되거나 위임되지 않았지만'오류'가 아님)을 알 수 있습니다.새 대상을 만들어서 속성을 의뢰할 때만 산열이 끊깁니다. 예를 들어 이 라인을 열 때 사용하는 예입니다.

토론 #5

문제 없어요.나는 문법이 너에게 어울려서 매우 기쁘다.당신은 이 문제를 해결할 수 있습니까?고맙습니다.

좋은 웹페이지 즐겨찾기