如何将部分布局与集合一起使用?导轨
我打电话:
render @users, :layout => "homepage"
因为我想用专门针对主页的自定义布局(views/users/_homepage.html.erb)包装用户的默认部分(views/_user.html.erb)。
但是,当我这样做时,我在 user.name 方法上收到 NoMethodError 。
由于某种原因,用户变量似乎没有在用户部分内正确初始化。
事实证明,经过一些测试,主页部分甚至没有被调用,它直接进入用户部分......
I am calling:
render @users, :layout => "homepage"
because I want to wrap the default partial for users (views/_user.html.erb) with a custom layout just for the homepage (views/users/_homepage.html.erb).
but, when I do this, I get the NoMethodError on the user.name method.
For some reason it seems like the user variable is not getting initialized properly inside the user partial.
It turns out after some test, the homepage partial is not even getting called, it is going straight to the user partial ....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是我想要的解决方案,我相信实际上可能有一种方法可以仅使用渲染调用来完成这项工作,但这就是给我正确输出的原因:
或者是:layout选项仅在渲染a时才起作用单一资源而不是集合?
This is not the solution I wanted, I believe there may actually be a way to make this work using just a call to render, but this is what gave me the correct output:
Or is it that the :layout option only works when rendering a single resource and not a collection?
截至 2019 年 9 月,在 Rails 6 中,我们是这样做的:
使用别名:
希望这可以帮助未来的搜索者,也帮助我将来。
As of Sept. 2019, in Rails 6, this is how we are doing this:
With alias:
Hope this helps future searchers, and also me in the future.
尝试添加
:as => :user
从视图中渲染部分内容:<%= 渲染:集合 => @用户,:as => :用户,:部分=> '用户/user_short_form', %>
Try adding
:as => :user
to render a partial from a view:<%= render :collection => @users, :as => :user, :partial => 'users/user_short_form', %>
你应该做一些类似
不确定 :layout 选项,但你必须通过 @users thro :collection
希望它有帮助!
You should do something like
not sure about the :layout option, but you have to pass @users thro :collection
hope it helps!