Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Way to add data for entire controller?

Controller View

  • Please log in to reply
4 replies to this topic

#1 GLaD0S

GLaD0S

    Passenger

  • Members
  • 6 posts

Posted 12 March 2014 - 09:37 PM

Is there a way to add data for the entire controller? For example if I placed @users as a class variable in the controller, would that make it available to all views that the controller handles without having to do anything further? Thanks for any help!



#2 GLaD0S

GLaD0S

    Passenger

  • Members
  • 6 posts

Posted 12 March 2014 - 10:50 PM

I have figured out that you can add a function with before_filter and include it that way. Is this a terrible method?


  • james likes this

#3 james

james

    Guard

  • Members
  • 223 posts
  • LocationLeeds, U.K.

Posted 13 March 2014 - 08:39 AM

filters are for exactly your requirement

In fact in Rails 4 now a generated controller uses a before action (previously known as a before filter) to set the params


  • GLaD0S likes this
Programming is just about problem solving!

#4 himanshu339

himanshu339

    Passenger

  • Members
  • 9 posts

Posted 17 March 2014 - 07:20 AM

yes, before_filter can be used to call a method before each action of a controller and in that method that variable can be initialized. This way the variable will be accessible in all view.

 

I hope this helps!


  • GLaD0S likes this

#5 GLaD0S

GLaD0S

    Passenger

  • Members
  • 6 posts

Posted 10 April 2014 - 12:45 AM

Thank you all for the help.







Also tagged with one or more of these keywords: Controller, View

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users