gpt4 book ai didi

ruby-on-rails - 应用程序 Controller 是否有关联的模板?

转载 作者:数据小太阳 更新时间:2023-10-29 07:35:25 26 4
gpt4 key购买 nike

假设我跑

rails new proj1

我可以

rails generate controller abc def

除此之外还创建了 .\app\views\abc\def.html.erb

我可以编辑 config.rb 说

获取'/',到:'abc#def'
或者
root 'abc#def'

当我转到 http://127.0.0.1:3000/

时,这将加载 def.html.erb 模板

但我很感兴趣是否可以在不创建新 Controller 的情况下做到这一点。我感兴趣的是我是否可以仅使用应用程序 Controller 转到模板。

因此,例如,我可以编辑 .\app\controllers\application_controller.rb 并添加

def a 
end

然后应用程序 Controller 的行为是否像其他 Controller 一样,并尝试呈现相应的 .html.erb 文件,例如'a' Action 会尝试呈现 a.html.erb 吗?如果是这样,我找不到应该将 a.html.erb 文件放在哪里?

其他 Controller 在 View 中有一个子目录,每个 Action 都在该子目录中有文件,可以手动或使用例如rails generate controller blah a b c 生成带有 Action 的 Controller ,并为每个 Action 生成一个模板。

但是我看不到应用程序 Controller 的那个。我看不到它的 View 在哪里。

最佳答案

你可以这样做:

路线.rb:

get '/', to: "application#root"

应用程序 Controller .rb

def root
end

app/views/application/root.html.erb

hello world

运行服务器并访问 localhost:3000 打印“hello world”

Sinatra 做的事情与此类似。路由和 Controller 操作基本上结合在一起,如果需要,您可以自己实现类似 rails 的 Controller 。

关于ruby-on-rails - 应用程序 Controller 是否有关联的模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48246517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com