在 Ruby on Rails 中,在视图中时,您可以在到达视图之前转储文件和行号吗?

发布于 2024-09-06 05:10:02 字数 275 浏览 4 评论 0原文

第一层是路由,第二层是控制器,所以当在视图中时,有没有办法在到达视图之前转储路由和控制器的路径或文件名和行号?

我想这样做是因为 www.example.com/stories/12345 正在显示一个页面,但 Stories_controller.rb 没有 def indexdef show

更新: 我猜想可能是程序有错误,在这种情况下它将显示应用程序跟踪和 Rails 跟踪。

The first layer is routing and the second layer the controller, so when in View, is there a way to dump out the path or filenames and line numbers of route and controller before reaching view?

I wanted to do this because www.example.com/stories/12345 is showing a page, but stories_controller.rb doesn't have a def index or def show

Update: I guess it might be as if the program has an error, in which case it will show an application trace and a rails trace.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画尸师 2024-09-13 05:10:02

实际上,除了使用 indexshow 之外没有其他选择,并且它必须在 StoriesController 类中定义。但这并不意味着这些方法是在文件“stories_controller.rb”中定义的。

有几个选项:

  • 它们在其派生的 ApplicationController 中定义。或者还有另一个父类。
  • 例如,当您使用 ActiveScaffold 时,您的控制器包含一行 active_scaffold,它添加了所有标准 CRUD 操作(索引、显示等)

要查看定义方法的位置,您可以在 script/console 会话中键入内容

StoriesController.new.method('index')

,它将返回一组类/模块,如下所述< a href="https://stackoverflow.com/questions/175655/how-to-find-where-a-ruby-method-is-define-at-runtime">此处。

例如,在我们的 ApplicationController 中,我们包含一个定义方法 login_required 的模块,看起来像这样:

>> ApplicationController.new.method(:login_required)
=> #<Method: ApplicationController(PilgrimBase::Base::Authenticated)#login_required>

希望这会有所帮助。

Well actually there is no other alternative than using a index or show and it has to be defined inside the class StoriesController. But it does not mean that those methods are defined inside the file "stories_controller.rb".

There are a few options:

  • they are defined in the ApplicationController from which it derives. Or there is another parent-class.
  • for instance, when you use ActiveScaffold your controller contains a single line active_scaffold that adds all the standard CRUD actions (index, show, ...)

To see where a method is defined, you could type inside your script/console session

StoriesController.new.method('index')

and it would return a set of classes/modules, as explained here.

For instance, in our ApplicationController we include a module that defines a method login_required, and that looks like this:

>> ApplicationController.new.method(:login_required)
=> #<Method: ApplicationController(PilgrimBase::Base::Authenticated)#login_required>

Hope this helps.

許願樹丅啲祈禱 2024-09-13 05:10:02

我知道这并不能真正回答你的问题,但是......
您可以通过执行以下操作来查看您的路线:

rake routes

您的 app/views/stories/ 中是否有 show.html.erb ?

I know this doesnt really answer your question, but ...
you can see your routes by doing

rake routes

do you have show.html.erb in your app/views/stories/ ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文