Newbie: Can I preselect pulldown option from passed in var?

Currently I have a list of clients with a link next to each one so I can
add a new task for that particular client.

When I click the link to go to the new task view page I’d like to retain
the clients_id so that it can be pre-selected in the pulldown menu which
allows me to assign a client otherwise.

I can see the client_id being passed in the url and I can render it on
the new task form page by using <%= @client_id %> but using a select
menu or collection_select the client_id is not preselected.

Specifically,

In my client list view I have this:
<%= link_to ‘add task’, { :action => ‘new’, :controller => “tasks”,
:client_id => client.id } %>

In my task controller in the new function definition I have:
@client_id = params[:client_id]

And in the task form I’m using this to create a pulldown menu of client
company names:
<%= select(‘task’, ‘client_id’, all_clients.collect {|p| [
p.company_name, p.id ] } ) %>

At first I was trying to use collection_select since it seemed easier
but that didn’t work either.

I hope this doesn’t seem too vague or basic or something to answer. Any
help or pointers appreciated.

DAN

If you use select(‘task’, ‘client_id’, …)
Then the drop down will preselect the value given by calling the
client_id method on @task, so set @task.client_id to the value passed
through the params and you should be ok.

Fred

Thanks!

Adding <% @task.client_id = @client_id %> above the form did the trick.

DAN

:select => @client_id in the html options hash

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000399

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs