简体   繁体   English

如何渲染两个具有相同名称的不同部分?

[英]How do I render two different partials with the same name?

I have我有

render @users

in my users view.在我的用户看来。 It renders the @users in a nicely formatted way that I specified in _user.html.erb .它以我在_user.html.erb中指定的格式良好的方式呈现@users I am now trying to do this:我现在正在尝试这样做:

render @attendees

which is a hash of users.这是hash的用户。 I am trying to render this in my events view (a different view).我正在尝试在我的事件视图(不同的视图)中呈现它。 The problem is that I want to render @attendees differently than @users but it always seems to use the _user.html.erb to render.问题是我想以不同于@users的方式render @attendees ,但它似乎总是使用_user.html.erb来呈现。 How do I specify a different rendering?如何指定不同的渲染?

As far as I'm aware, you can't use the convenience of render @attendees to render a different partial than _user since @attendees are actually User objects.据我所知,由于@attendees实际上是User对象,因此您不能使用render @attendees_user不同的部分。 Rails uses the class to figure out what partial to render when using this condensed format. Rails 使用 class 来确定在使用这种压缩格式时要渲染的部分。

What you can do is render:partial => 'users/attendees', :collection => @attendees , assuming you have a partial users/_attendees.html.erb .你可以做的是render:partial => 'users/attendees', :collection => @attendees ,假设你有部分users/_attendees.html.erb If you don't want to throw that around everywhere, wrap it in a helper.如果您不想到处乱扔它,请将其包裹在助手中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM