开发者

Rendering multiple actions' results in one request on rails

I am using Ruby on Rails and am trying to load a page which consists of many sub-pages. Each sub-page is pretty different and the data for each sub-page is generated by a separate action in the controller. Upon loading the main page, I want to also load the content for the sub-pages by calling the corresponding actions.

So my question is whether it is possible to fire off multiple requests to different actions upon a single page request. If so, what is the best way to achieve this so that the resulting views from each action are displayed in the right parts of the page? If it's not possible, how else can I load content processed from different actions in a single request?

Ideally, the actions for sub-pages can be called from different places (e.g. the sub-page can be a component of a di开发者_开发技巧fferent pages and could also be its own page). I want to abstract out the place from where an action is being called from the controller so I can simply render the appropriate partial for the data generated by the controller. But the key is that this partial is not going to be determined in the controller, but rather in the view. Has something like this been done? Is there an even better alternative?

Thanks!


What you want to do should not by solved by calling different actions.

In the controller, you know which models you want to display in the view, so you set up the attributes accordingly, e.g.

@user = current_user
@team = current_user.team.first
@top_teams = Team.top5 # this is a scope returning the 5 top teams

In your view, you then use partials to render the different parts of your site

<%= render "users/parts/box", :user => @user %>
<%= render "teams/parts/detail", :team => @team %>
<%= render "teams/parts/top", :collection => @top_teams %>

I suggest you have a look at Opensourcerails, which itself is open source and makes use of this technique. A good example is the user's show action.


I don't exactly know what you want. But definitely you cannot render other action with views as a part of the current action.

However, you could write private methods on the controller for setting up the variables and include the corresponding common partials in your view files.

This is an example:

class UsersController < ...
  def show
    @user = current_user

    setup_user_friends_widget(@user)
  end

  private

  def setup_user_friends_widget(for_who)
    @use_user_friends_widget= true

    @friends = for_who.friends
  end
end

# in your view users/show.html.erb

<%- if @use_users_friend_widget %>
  <%= render :partial => "shared/widgets/user_friends"
<%- end %>

# in shared/widgets/_user_friends

<%- @friends.each do |friend| %>
  # use the instance variables directly because you have to call the setup_user_friends_widget method before you could use this widget.
  # so you are safe to use the instance variables instead of passing them from render :partial

  ...
%>

In this way, you could extract those common-things out to private methods on controllers and those methods automatically have access to params, sessions, cookies if you need them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜