Friday 15 March 2013

model view controller - ruby on rails best place to put layout database code (common database calls) -


I am creating a control panel (user accounts) in the rail, I need to show things like messages or notifications in the layout (Style like Facebook) The problem is that these things require access to the database and I am not sure where the code is kept because it is not related to any controller, but the layout is shared with many controllers is.

The code should take place to fetch messages from the database, I should put it in the layout (I do not feel right), or as an assistant?

The best solution was to create a control panel controller that handles authentication and permissions and messages Just like the normal user loads the data ... here is an example code

  class controlPanelController & lt; ApplicationController before_filter: authenticate_user! Before_filter: get_user_data helper_method: mailbox authorize_resource protected def get_user_data @ header_conversations = mailbox.inbox.limit (3) @ uevents = Event.scoped @ uevents = @ uevents.after (time.nu) enddef mailbox @mailbox || = Current_user.mailbox   

and then all classes in my web application extend this class :)

No comments:

Post a Comment