在rails應用程序中,在使用byebug
, 控制器的操作方法設置斷點後,當我鍵入一些gibberesh時,我發現在打印之前,我的application_controller.rb中的方法會自動執行「未定義的局部變量或方法」在rails中調試調用者
這種魔法是怎麼發生的?我如何追查這個......我不希望這種方法運行時胡言亂語鍵入...
(byebug) abcd1234abcd1234
WpPost Load (4.8ms) SELECT `wp_posts`.`post_title`, `wp_posts`.`post_date`, `wp_posts`.`post_name` FROM `wp_posts` WHERE `wp_posts`.`post_status` = 'publish' AND (post_date_gmt >= '2017-01-30 23:31:52.437270') ORDER BY post_date DESC LIMIT 3
*** NameError Exception: undefined local variable or method `abcd1234abcd1234' for #<FooController:0x007fa717745bd8>
nil
(byebug)
一些代碼
# foo_controller.rb
class FooController < ApplicationController
def show
byebug # I type abcd1234abcd1234 at this prompt
# ...
# application_controller.rb
class ApplicationController < ActionController::Base
before_filter :something
before_filter :something2
before_filter :load_blog_posts, :something3
def ....
end
def load_blog_posts
@wp_posts = WpPost.where(post_status: 'publishh')
.where("post_date_gmt >= ?", Time.now - 1.week)
.order("post_date DESC")
.limit(3)
.select(:post_title, :post_date, :post_name)
end
有趣的狀態,我的應用程序控制器有很多before_filters,最後一個過濾器實際上是一個調用引用WpPost的方法... –
是的,它絕對是導致此行爲的過濾器......是否解決了您的問題? –
不存在問題...我無法刪除該過濾器,因爲它是我們應用程序的關鍵部分。 –