Rails 不需要在控制器中定义索引方法? [英] Rails doesn't need index method in controller defined?

查看:24
本文介绍了Rails 不需要在控制器中定义索引方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,即使没有控制器方法索引,索引视图也能正确路由.

I noticed that an index view is routed correctly even if there isn't a controller method index.

例如,routes.rb 有这个路由

AppName::Application.routes.draw do
  get 'about' => "about#index"
end

我的控制器看起来像这样没有索引方法(def index end)

My controller looks like this with no index method (def index end)

class AboutController < ApplicationController
end

我在 views/about 文件夹中有一个名为 index.html.erb 的视图

and I have a view called index.html.erb in the views/about folder

这里发生了什么?这是 Rails 魔法的一种情况,即使没有控制器方法,它们也会自动显示视图?我找不到关于此的任何文档...

What's happening here? Is this a case of rails magic where they automatically show the view even if there is no controller method? I couldn't find any documentation on this...

推荐答案

如果你有视图文件,它会继续隐式渲染,如文档所述 此处

If you have the view file, it'll go ahead and render that implicitly, as documented here

另请参阅Rails 如何呈现您的视图文件和控制器操作.

这篇关于Rails 不需要在控制器中定义索引方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆