2

I want to build an app which handles multiple pages, and the user can add to the app (from a form) a page, then add the added page to a pages table. (Normally)

All of this I know how to do and I don't have any issues at it.

The problem is that I want to create a menu in application.html.erb, but I need to get the pages table data from a controller, so I tried to pass regular instance variables from application_controller.rb to application.html.erb, and it didn't work.

In application_controller.rb:

    @foo = "bar"

In application.html.erb:

   <%= @foo %>

I don't even get an error, there is just nothing in the view. (I mean the variable isn't there, I can still see other content)

How can I attach a controller for application.html.erb? (Or pass variables to application.html.erb) Thanks for any help.

SOLUTION: In you application_controller you can do

    before_action :set_page


    def set_page
      @foo = "bar"
    end

This will run the set_page action before every action in your application which should accomplish what you want.

Thanks to ruby_newbie!!!!

1 Answer 1

3

In you application_controller you can do

before_action :set_page


def set_page
  @foo = "bar"
end

This will run the set_page action before every action in your application which should accomplish what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It is working! +1 and I'll mark your answer as the solution in 9 mins... :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.