Rails 控制器:可以嵌套渲染视图吗?

发布于 2025-01-02 13:29:21 字数 497 浏览 1 评论 0原文

我正在做 Lynda.com Rails 教程,他们解释了如何使用 render('methodname') 渲染默认视图之外的另一种视图。

但是,我注意到这个渲染不是“嵌套的”。例如,在下面的代码中,localhost:3000/demo/index将在views/demo/hello.html.erb中生成视图,而localhost:3000/demo/hello将呈现文本“Hello there”。

有没有办法进行“嵌套”渲染,即在本例中请求演示/索引将返回“Hello there”?

(另外,嵌套渲染的一些用例会很好。我只是出于好奇才问。)

class DemoController < ApplicationController
  def index
     render ('hello')            
  end

  def hello
    render(:text => 'Hello there')
  end

end

I am doing the Lynda.com rails tutorial and they explain how to render another view than the default one using render('methodname').

However, I noticed this rendering is not "nested". For example, in the code below, localhost:3000/demo/index would generate the view in views/demo/hello.html.erb, while localhost:3000/demo/hello would render the text 'Hello there'.

Is there a way to have "nested" rendering, i.e. such that requesting demo/index would return 'Hello there' in this example?

(Also, some use cases for nested rendering would be nice. I am asking only out of curiosity.)

class DemoController < ApplicationController
  def index
     render ('hello')            
  end

  def hello
    render(:text => 'Hello there')
  end

end

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

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

发布评论

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

评论(2

隔纱相望 2025-01-09 13:29:21

我不知道你所说的嵌套渲染到底是什么意思。

场景#1

如果您希望触发操作“index”但显示模板“hello.html.erb”,您可以这样做

def index
  render :action => :hello
end

这将渲染模板app/views/demos/ hello.html.erb (或其他格式,如果你想要它(即在网址中指定它))。

所以渲染:动作=> :hello 只是一个快捷方式。

您还可以执行 render :template =>; “hello.html.erb”render :file => Rails.root.join("app/views/demos/hello.html.erb") (有时有用)。

场景 #2

如果你想渲染该文本,你可以在索引方法中调用 hello 方法

def index
  hello
end

如果你不想运行 hello 操作中的其他内容,你可以将其与其他方法分开,例如所以:

def render_hello
  render :text => "Hello world"
end

def index
  # some other stuff going on...
  render_hello
end

def hello
  # some other stuff going on...
  render_hello
end

你不能在同一个动作中渲染两次。

顺便说一句,URL 不应显示 /demos/index,而应显示 /demos
索引是 resources 路由 (resources :demos) 的默认操作。

请选择适合您的场景(以便我可以从此答案中删除不必要的文本)。

I don't know what you exactly mean by nested rendering.

Scenario #1

If you want action "index" to be triggered but template "hello.html.erb" to be shown, you can do

def index
  render :action => :hello
end

This will render the template app/views/demos/hello.html.erb (or other format if you want it to (i.e. specify it in the url)).

So render :action => :hello is just a shortcut.

You can also do render :template => "hello.html.erb" or render :file => Rails.root.join("app/views/demos/hello.html.erb") (sometimes useful).

Scenario #2

If you want to render that text, you can just call hello method inside index method

def index
  hello
end

If you don't want other stuff from hello action to be run you can separate it to other method, like so:

def render_hello
  render :text => "Hello world"
end

def index
  # some other stuff going on...
  render_hello
end

def hello
  # some other stuff going on...
  render_hello
end

You can not render twice in the same action.

Btw the url should not say /demos/index but just /demos.
Index is the default action for resources route (resources :demos).

Please select the scenario which suits you (so I can remove unnecessary text from this answer).

霞映澄塘 2025-01-09 13:29:21

您当前正在尝试在控制器中渲染,所有渲染都应该在 Rails 的视图中处理。

的文件中

因此,对于上面的结构,您的 DemoController 应位于app/controllers/demo_controller.rb

,并且将呈现的视图将位于以下文件中:

app/views/demo/index.html.erb

app/ views/demo/_hello.html.erb (文件名 _hello.html.erb 上的前导下划线向 Rails 表明这是要在另一个页面中呈现的“部分”

) index.html.erb 文件,您将调用 hello.html.erb 文件的渲染。您生成的代码应如下所示:

demo_controller.rb

class DemoController < ApplicationController

  def index          
  end

end

index.html.erb

<%= render 'demo/hello' %>

_hello.html.erb

<p>Hello there</p>

You're currently trying to render in a controller, all rendering should be handled in the views in Rails.

So for your structure above, your DemoController should be located in a file at

app/controllers/demo_controller.rb

and the views that will be rendered will be in files located at:

app/views/demo/index.html.erb

and

app/views/demo/_hello.html.erb (The leading underscore on the file name _hello.html.erb indicates to Rails that this is a "partial" to be rendered inside another page)

Inside the index.html.erb file you would call a render to the hello.html.erb file. Your resulting code should look like this:

demo_controller.rb

class DemoController < ApplicationController

  def index          
  end

end

index.html.erb

<%= render 'demo/hello' %>

_hello.html.erb

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