带有布局和部分的双重渲染
好的,我有一个尝试渲染布局的显示方法,并且显示视图渲染了几个部分,但我在 Rails 中遇到了双重渲染错误。如何让它渲染布局和部分?请注意,它在默认布局下渲染得很好。
这是我的控制器操作
def show
@site = Site.find_by_subdomain!(request.subdomain)
@page = @site.pages.find_by_name('index')
render :layout => "layouts/mobile"
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @site }
end
end
,我的视图如下所示:
<%= render(:partial => "page", :object => @page) %>
Ok so I have a show method that tries to render a layout and the show view renders a couple partials but I am getting a double render error in rails. How do I get it to render the layout and the partials? Note that it renders just fine with the default layout.
Here is my controller action
def show
@site = Site.find_by_subdomain!(request.subdomain)
@page = @site.pages.find_by_name('index')
render :layout => "layouts/mobile"
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @site }
end
end
And my view simply looks like this:
<%= render(:partial => "page", :object => @page) %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,明白了。很抱歉发了这个帖子,但如果其他人遇到问题,我会回答它。基本上在我的控制器文件的顶部我只需要这一行:
Ok figured it out. Sorry for the post but I'll answer it in case someone else has the problem. Basically at the top of my controller file I just needed this line:
不确定为什么您在调用它的确切位置在操作中调用渲染。简而言之:您不需要在那里执行此操作。如果这样做,当该方法返回控制权时,rails 将尝试再次渲染,因此您要么必须在调用 render 之后从该方法返回,要么不在该方法中调用它。
Not sure why you call render in your action at that exact place where you call it. In short: You shouldn't need to do it there. If you do it, rails will try to render again when the method returns control, so you either have to return from the method after calling render or not call it in the method.