How to pass local variable to partial from application layout in rails 3?

I have a couple of variables that need to be called in all controllers. Display the latest news in the footer of the layout.

I create them in application_controller.rb

@hq_news_item = NewsItem.where(:branch_code => "CORP").first
@branch_news_item = NewsItem.where(:branch_code => "MN").first

In my mockups /application.html.haml

= render :partial => "layouts/footer_news"  , :hq_news_item => @hq_news_item, :branch_news_item => @branch_news_item

And then in my mockups / _footer_news I style them

= hq_news_item.title
= hq_news_item.author.name
... etc

That's what, no matter what I do - he keeps saying that hq_news_item is undefined in partial.

All my other partial works are working fine. I think this is due to the fact that this is a layout, not a view. I can not find anything meaningful in the documents.

Any ideas?

Thank.

+3
source share
2 answers

I think you need to pass the variables as local variables to partial:

= render :partial => "layouts/footer_news", :locals => { :hq_news_item => @hq_news_item, :branch_news_item => @branch_news_item }

Rails , , , .

+7

- @hq_news_item?

, locals, , .

rails 2.3.8

<% f.fields_for :member_collection do |builder| %> 

  <%= render "membrer_collection_fields", :form => builder %> 

<% end %>

_member_collection_fields,

+1

All Articles