0

today I wanted to use some variables in my shared/menu partial. I know that I should put somewhere in application_controller some helper method (or something like that) but it doesn't work for me. Here is my temporary solution (shared/_menu.html.haml):

- @pagecontents = Pagecontent.all
- @pagecontents.each do |pc|
  %ul
    %li
      = link_to pc.title, pc

I don't want to define @pagecontents variable directly in my view! :/ My problem is to add @pagecontent variable somewhere to application_controller file but when I tried e.g:

def pagecontentforpartial
  @pagecontents = Pagecontent.all
end

and then tried to access this variable in my partial view I got an error that this method is nil (but there is already a lot of records!) Someone can explain me how solve this problem?

lukaszkups
  • 5,790
  • 9
  • 47
  • 85

1 Answers1

1

You can pass object to your partial directly using render method

It's your shared/_menu.html.haml:

- pagecontents.each do |pc|
%ul
  %li
    = link_to pc.title, pc

And when you need to show it, you can use this method in your view

render 'shared/menu', pagecontents: @pagecontents

@pagecontents is from controller, of course

caulfield
  • 1,383
  • 1
  • 11
  • 21