Ruby-on-rails Rails:从视图内渲染视图(不是局部)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16328472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: Render a View (not a partial) From Within a View
提问by nullnullnull
I have a controller that responds to both htmland js. The htmlview renders the whole page (including the header and footer), while the jsonly replaces #main. Aside from the header and footer, both formats render the same content. I can get this effect with three files:
我有一个控制器可以同时响应html和js。该html视图使得整个页面(包括页眉和页脚),而js只替换#main。除了页眉和页脚之外,两种格式呈现相同的内容。我可以通过三个文件获得这种效果:
_show.html.erb
<div>Content!</div>
show.html.erb
<%= render "show" %>
show.js.erb
$("#main").fadeIn("<%= escape_javascript(render 'show') %>");
This works, but I'd prefer if I didn't need a separate _showpartial. Unfortunately, this doesn't work:
这有效,但如果我不需要单独的_show部分,我更喜欢。不幸的是,这不起作用:
show.html.erb
<div>Content!</div>
show.js.erb
$("#main").fadeIn("<%= escape_javascript(render 'show') %>");
As Rails will look for the showpartial, not the actual view.
因为 Rails 会寻找show局部视图,而不是实际视图。
Is there a way to get Rails to look for the view file, rather than a partial?
有没有办法让 Rails 查找视图文件,而不是部分文件?
采纳答案by numbers1311407
Rendering a non-partial view inside another view isn't exactly the Rails Way?. Your current solution is probably better, and not an uncommon approach. Rename it _body, or something else appropriate, if you feel weird about the the partial name being the same as the action.
在另一个视图中渲染一个非局部视图不完全是 Rails 方式吗?。您当前的解决方案可能更好,而且不是不常见的方法。如果您对部分名称与操作相同感到奇怪,请将其重命名为 _body 或其他适当的名称。
However if your view can be shared, as it seems like it could in this case, you could just make it a layout.
但是,如果您的视图可以共享,就像在这种情况下一样,您可以将其设为布局。
This is facilitated by the fact that, somewhat against the principle of least surprise, Rails will render an htmltemplate for a jsaction if no jstemplate exists. This means that you could remove both the js template, and the partial, and just create a layout entitled, for example, fadein.js.erb:
这是通过,有些对最小惊讶的原则,Rails会渲染这个事实促成html模板的js,如果不采取行动js模板存在。这意味着您可以删除 js 模板和部分模板,然后创建一个名为的布局,例如fadein.js.erb:
# yourviews/show.html.erb
<div>Content!</div>
# layouts/fadein.js.erb
$("#main").fadeIn("<%= escape_javascript(yield) %>");
# YourController.rb
def show
# ...
respond_to do |wants|
wants.html
wants.js { render :layout => "fadein" }
end
end
回答by Tikiboy
Like others have mentioned, rendering another non-partial view in another view is "Not the rails way", if however you still insist on doing it, one method is:
就像其他人提到的那样,在另一个视图中渲染另一个非局部视图是“不是轨道方式”,但是如果您仍然坚持这样做,一种方法是:
<%= render :file => '<replace with relative/absolute path>' %>
回答by fotanus
This is not a good practice as you can see by the comments. Rails have the concepts of view, partialand layout. That said, the view is the only one you should keep using only once. So my suggestions are:
正如您在评论中看到的那样,这不是一个好习惯。Rails 有view、partial和layout的概念。也就是说,视图是唯一应该只使用一次的视图。所以我的建议是:
- If you feel that more than one extra view could be rendered inside your current view, you are most likely looking for a layout
- If you feel that one of your views should be rendered in many pages, you are looking for a partial
- If this view should render only one extra view inside it, and that view should only be rendered inside the current view, you can pick any of the aboveor none of it- that's it, go with a single file
- 如果您觉得当前视图中可以渲染多个额外视图,那么您很可能正在寻找一种布局
- 如果你觉得你的一个视图应该在许多页面中呈现,你正在寻找一个部分
- 如果这个视图应该只在其中渲染一个额外的视图,并且该视图应该只在当前视图内渲染,您可以选择上述任何一个或不选择任何一个- 就是这样,使用单个文件
回答by GMsoF
you just need to pass a context to your rendermethod:
您只需要将上下文传递给您的render方法:
<%= render :template => "show" -%>

