Issue
I have a couple of variables that need to be called in all controllers. Displaying latest news in the layout footer.
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 layouts/application.html.haml
= render :partial => "layouts/footer_news" , :hq_news_item => @hq_news_item, :branch_news_item => @branch_news_item
And then in my layouts/_footer_news I style them
= hq_news_item.title
= hq_news_item.author.name
... etc
Here is the thing, no matter what I do - it keeps saying that hq_news_item is undefined in partial.
All my other partials work fine. I think it has to do with the fact that it's a layout not a view. Can't find anything meaningful in the docs.
Any ideas?
Thank you.
Solution
I think you need to pass the variables as local variables to the partial:
= render :partial => "layouts/footer_news", :locals => { :hq_news_item => @hq_news_item, :branch_news_item => @branch_news_item }
Otherwise Rails won't really understand what you are passing as a variable to the partial and what you are passing as an argument to the render function.
Answered By - Pan Thomakos Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.