I have a "Users" object and a "Timesheets" object. Each timesheet entry belongs to a User, which is selected from a drop-down list. The timesheet has a user_id field. (The user needs to have an option to select a different user due to the way our system works.) If i use the "select" object, which is bound to the Timesheet model, using this code: <%= select ''timesheet'', ''user_id'', User.find_all.collect {|p| [p.username, p.id]} %> Then the list defaults to the first option. I''d like it to default to the logged in user though. I can do this using select_tag with options_from_collection_for_select without a problem, but then I lose the functionality of having the field linked to the table. If i use a select_tag, I''m forced to add additional code in the Create method to get the user_id into the new record (which just doesn''t seem to follow the kiss/dry principles). Thanks! _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
One way would be to put @timesheet.user_id session[:logged_in_user_id_or_whatever] in the controller action for this view. select() just defaults the selection to the current value returned by the method. In this case, @timesheet.user_id On 1/18/06, Dylan Markow <dylan@dylanmarkow.com> wrote:> I have a "Users" object and a "Timesheets" object. Each timesheet entry > belongs to a User, which is selected from a drop-down list. The timesheet > has a user_id field. (The user needs to have an option to select a different > user due to the way our system works.) > > If i use the "select" object, which is bound to the Timesheet model, using > this code: > <%= select ''timesheet'', ''user_id'', User.find_all.collect {|p| [p.username, > p.id]} %> > Then the list defaults to the first option. I''d like it to default to the > logged in user though. I can do this using select_tag with > options_from_collection_for_select without a problem, but > then I lose the functionality of having the field linked to the table. If i > use a select_tag, I''m forced to add additional code in the Create method to > get the user_id into the new record (which just doesn''t seem to follow the > kiss/dry principles). > > Thanks! > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Perfect! Thanks. Wilson Bilkovich wrote: One way would be to put @timesheet.user_id session[:logged_in_user_id_or_whatever] in the controller action for this view. select() just defaults the selection to the current value returned by the method. In this case, @timesheet.user_id On 1/18/06, Dylan Markow wrote: I have a "Users" object and a "Timesheets" object. Each timesheet entry belongs to a User, which is selected from a drop-down list. The timesheet has a user_id field. (The user needs to have an option to select a different user due to the way our system works.) If i use the "select" object, which is bound to the Timesheet model, using this code: <%= select ''timesheet'', ''user_id'', User.find_all.collect {|p| [p.username, p.id]} %> Then the list defaults to the first option. I''d like it to default to the logged in user though. I can do this using select_tag with options_from_collection_for_select without a problem, but then I lose the functionality of having the field linked to the table. If i use a select_tag, I''m forced to add additional code in the Create method to get the user_id into the new record (which just doesn''t seem to follow the kiss/dry principles). Thanks! _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails