开发者

Rendering the output of action in template

Is there some way to 开发者_JAVA百科output an action in Rails template? Something equivalent to ASP.NET MVC Html.RenderAction ? I need to render some stuff in sidebar and I don't want to put queries in partials or specific controller. So far I can think of only one way - put something into @stuff (by whatever means) instance var and let render find the proper partial or specify it explicitly. It would be better to be able to change only one file to change the contents of sidebar (as in ASP).


Yes, there is. You can add the "sidebar" partial to the application layout (in the app/views/layouts folder).

You can put the code that gets the "sidebar" variables in a before_filter in the ApplicationController

class ApplicationController < ActionController::Base
  before_filter :sidebar_function


have a look at

http://guides.rubyonrails.org/layouts_and_rendering.html

especially read the subitem

http://guides.rubyonrails.org/layouts_and_rendering.html#using-content_for


I do not know ASP.net, however, I think Rails yield might be the solution. Here is a small example:

view:

<% content_for :one do %>
  Test one
<% end %>

<% content_for :two do %>
  Test two
<% end %>

<p>Hi</p>

application.html.erb

<%= yield :one %>
<%= yield %>
<%= yield :two %>

See Railsguides: http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜