Set a default for a <select-one> field

43 views
Skip to first unread message

Bob Sleys

unread,
Sep 29, 2011, 3:13:36 PM9/29/11
to hobo...@googlegroups.com
I have stored in the session the value I'd like to use as the default for a <select-one> tag but I don't see any documentation on how to do this.  I checked the hobo manual and it doesn't show a param to set for this.  Basically the problem is when a user interacts with my app thy are working on a particular project, ie recording all the work associated with a project.  I want to allow them to choose a default project and then use that default anyplace they are adding new data that requires a project so they don't have to select it over and over again.  I've already got the code to select the project they are working on and keep it stored in their session now I just need to default the <select-one> tag for the project field to that project.  Any hints would be appreciated.

Thanks
Bob

Henry Baragar

unread,
Sep 29, 2011, 3:28:25 PM9/29/11
to hobo...@googlegroups.com

Bob,


Use the standard Ruby initialize method, something like:

def initialize *args

super *args

self.project ||= owner.default_project

end


Cheers,

Henry


--

Henry Baragar

Instantiated Software

416-907-8454 x42

Bob Sleys

unread,
Oct 7, 2011, 12:29:49 PM10/7/11
to hobo...@googlegroups.com
I tried this approach but I'm having a problem in that where I'm storing the default value isn't available in the model.

I'm basically duplicating the @current_user code and setting a @current_project session variable via a before_filter in the ApplicationControler.

In my Sample model I have the following code

  after_initialize :init
  
  def init
    self.project_id ||=  @current_project
  end

However @current_project is nil.  It's not set.  I've even tried to directly access the session via session[:project] but that also throws a nil value exception and debugging show the session doesn't exist at this point of execution.

Bob

Bob Sleys

unread,
Oct 10, 2011, 11:04:23 AM10/10/11
to hobo...@googlegroups.com
Ok this one is really bugging me because it seems like it should be so simple to do.  I have in a the session and in a session variable the default value I want to use for a select list but can't find a way to use it as the default for the select list.  The previously posted code doesn't work because the session and session variable are empty, ie nil at the point the after_initialize is run and the select list tag doesn't have an option to pass it a default value as far as I can tell.

Bob

kevinpfromnm

unread,
Oct 10, 2011, 3:43:42 PM10/10/11
to hobo...@googlegroups.com
The problem with session is it's a controller level construct which is not available at the model level.  In order to pass that information to the model, you'll need to modify the controller action that's setting up the model.
Reply all
Reply to author
Forward
0 new messages