2014-10-16 65 views
0

所以我有我的應用程序中的用戶,媒體和評論。 用戶和媒體都有一個 has_many :comments。 評論模式有一個belongs_to爲用戶和媒體。has_many dependent:銷燬'參數的錯誤數量(0代表1)'

現在我在用戶和媒體上的has_many之後也有dependent: destroy選項。

現在,當我進入索引頁,對於用戶來說,一切都很好,但是當我進入索引頁的媒體,它會扔在has_many方法錯誤:wrong number of arguments (0 for 1)

所有型號都有其相應的控制器,具有相應的方法定義如destroy方法等。

這裏有什麼問題?

媒體模式:

class Media < ActiveRecord::Base 
    belongs_to :user 
    before_create :randomize_file_name 

    private 
     def randomize_file_name 
      extension = File.extname(image_video_file_name).downcase 
      self.image_video.instance_write(:file_name, "#{SecureRandom.hex(8)}#{extension}") 
     end 

    has_many :comments, dependent: destroy 
end 

用戶模式:

class User < ActiveRecord::Base 
    validates :name, presence: true, length: {maximum: 50} 

    enum role: [:user, :moderator, :admin] 
    after_initialize :set_default_role, :if => :new_record? 

    def set_default_role 
     self.role ||= :user 
    end 

    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
      :recoverable, :rememberable, :trackable, :validatable 

    has_many :medias, dependent: :destroy 
    has_many :comments, dependent: :destroy 
end 

評論型號:

class Comment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :media 

    validates :text, presence: true, length: { maximum: 255 } 
    validates :user_id, presence: true 
    validates :media_id, presence: true 
end 

完整的錯誤:

ArgumentError - wrong number of arguments (0 for 1): 
    activerecord (4.1.6) lib/active_record/querying.rb:8:in `destroy' 
    app/models/media.rb:19:in `<class:Media>' 
    app/models/media.rb:1:in `<top (required)>' 
    activesupport (4.1.6) lib/active_support/dependencies.rb:443:in `block in load_file' 
    activesupport (4.1.6) lib/active_support/dependencies.rb:633:in `new_constants_in' 
    activesupport (4.1.6) lib/active_support/dependencies.rb:442:in `load_file' 
    activesupport (4.1.6) lib/active_support/dependencies.rb:342:in `require_or_load' 
    activesupport (4.1.6) lib/active_support/dependencies.rb:480:in `load_missing_constant' 
    activesupport (4.1.6) lib/active_support/dependencies.rb:180:in `const_missing' 
    activesupport (4.1.6) lib/active_support/dependencies.rb:512:in `load_missing_constant' 
    activesupport (4.1.6) lib/active_support/dependencies.rb:180:in `const_missing' 
    app/controllers/media_controller.rb:5:in `index' 
    actionpack (4.1.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action' 
    actionpack (4.1.6) lib/abstract_controller/base.rb:189:in `process_action' 
    actionpack (4.1.6) lib/action_controller/metal/rendering.rb:10:in `process_action' 
    actionpack (4.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action' 
    activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call' 
    activesupport (4.1.6) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional' 
    activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `block in halting' 
    activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting' 
    activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting' 
    activesupport (4.1.6) lib/active_support/callbacks.rb:229:in `block in halting' 
    activesupport (4.1.6) lib/active_support/callbacks.rb:166:in `block in halting' 
    activesupport (4.1.6) lib/active_support/callbacks.rb:86:in `run_callbacks' 
    actionpack (4.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action' 
    actionpack (4.1.6) lib/action_controller/metal/rescue.rb:29:in `process_action' 
    actionpack (4.1.6) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action' 
    activesupport (4.1.6) lib/active_support/notifications.rb:159:in `block in instrument' 
    activesupport (4.1.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
    activesupport (4.1.6) lib/active_support/notifications.rb:159:in `instrument' 
    actionpack (4.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action' 
    actionpack (4.1.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action' 
    activerecord (4.1.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action' 
    actionpack (4.1.6) lib/abstract_controller/base.rb:136:in `process' 
    actionview (4.1.6) lib/action_view/rendering.rb:30:in `process' 
    actionpack (4.1.6) lib/action_controller/metal.rb:196:in `dispatch' 
    actionpack (4.1.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch' 
    actionpack (4.1.6) lib/action_controller/metal.rb:232:in `block in action' 
    actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:82:in `dispatch' 
    actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:50:in `call' 
    actionpack (4.1.6) lib/action_dispatch/journey/router.rb:73:in `block in call' 
    actionpack (4.1.6) lib/action_dispatch/journey/router.rb:59:in `call' 
    actionpack (4.1.6) lib/action_dispatch/routing/route_set.rb:678: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.5.2) lib/rack/etag.rb:23:in `call' 
    rack (1.5.2) lib/rack/conditionalget.rb:25:in `call' 
    rack (1.5.2) lib/rack/head.rb:11:in `call' 
    actionpack (4.1.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call' 
    actionpack (4.1.6) lib/action_dispatch/middleware/flash.rb:254:in `call' 
    rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context' 
    rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call' 
    actionpack (4.1.6) lib/action_dispatch/middleware/cookies.rb:560:in `call' 
    activerecord (4.1.6) lib/active_record/query_cache.rb:36:in `call' 
    activerecord (4.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call' 
    activerecord (4.1.6) lib/active_record/migration.rb:380:in `call' 
    actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' 
    activesupport (4.1.6) lib/active_support/callbacks.rb:82:in `run_callbacks' 
    actionpack (4.1.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
    actionpack (4.1.6) lib/action_dispatch/middleware/reloader.rb:73:in `call' 
    actionpack (4.1.6) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' 
    better_errors (2.0.0) lib/better_errors/middleware.rb:84:in `protected_app_call' 
    better_errors (2.0.0) lib/better_errors/middleware.rb:79:in `better_errors_call' 
    better_errors (2.0.0) lib/better_errors/middleware.rb:57:in `call' 
    actionpack (4.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' 
    actionpack (4.1.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
    railties (4.1.6) lib/rails/rack/logger.rb:38:in `call_app' 
    railties (4.1.6) lib/rails/rack/logger.rb:20:in `block in call' 
    activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `block in tagged' 
    activesupport (4.1.6) lib/active_support/tagged_logging.rb:26:in `tagged' 
    activesupport (4.1.6) lib/active_support/tagged_logging.rb:68:in `tagged' 
    railties (4.1.6) lib/rails/rack/logger.rb:20:in `call' 
    quiet_assets (1.0.3) lib/quiet_assets.rb:23:in `call_with_quiet_assets' 
    actionpack (4.1.6) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
    rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.5.2) lib/rack/runtime.rb:17:in `call' 
    activesupport (4.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' 
    rack (1.5.2) lib/rack/lock.rb:17:in `call' 
    actionpack (4.1.6) lib/action_dispatch/middleware/static.rb:64:in `call' 
    rack (1.5.2) lib/rack/sendfile.rb:112:in `call' 
    railties (4.1.6) lib/rails/engine.rb:514:in `call' 
    railties (4.1.6) lib/rails/application.rb:144:in `call' 
    passenger (4.0.53) lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request' 
    passenger (4.0.53) lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request' 
    passenger (4.0.53) lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop' 
    passenger (4.0.53) lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads' 
+0

你到底要與該說些什麼? – Kaspar 2014-10-16 14:08:27

+0

我們在這裏需要更多的東西 - 你能向我們展示模型和驗證以及視圖中正在炸燬的線嗎? – Anthony 2014-10-16 14:09:51

+0

視圖中沒有線條被炸燬。除了模型外,只有媒體索引中的'@ media = Media.all'包含錯誤。此外,更新線程。 – Kaspar 2014-10-16 14:15:45

回答

4

您的media類一個錯字,:destroy需要一個符號:

class Media < ActiveRecord::Base 
    belongs_to :user 
    before_create :randomize_file_name 

    private 
     def randomize_file_name 
      extension = File.extname(image_video_file_name).downcase 
      self.image_video.instance_write(:file_name, "#{SecureRandom.hex(8)}#{extension}") 
     end 

    has_many :comments, dependent: :destroy 
end 
+0

多麼愚蠢的錯誤......幾個小時沒有看到它。謝謝! – Kaspar 2014-10-16 14:18:38

相關問題