PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

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

 May 18, 2022     layout, partial, partial-views, ruby-on-rails     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing