I have a select list which is filled with options from the database
using the following code
<%= collection_select(:client, :name, @clients, :id, :name) %>
If I select an option from the list, but then miss out one of my other
validation requirements the list gets reset when the form renders with
the errors. I''m sure this is because of my create action which has the
code
	  def create
	    @image = Image.new(params[:image])
	    @categories = Category.find_all
	    @clients = Client.find_all
           
           ...[snip]
	  end
However if I dont recreate the variables I get errors.
Basically what I asking is can you keep your selected option when you
re-render your form with errors?
Any help/advice would be great.
Many Thanks
Kieran
Sorry to bug, but this one''s really got me stuck... On 7/19/05, Kieran Johnson <endlessinfinity-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a select list which is filled with options from the database > using the following code > > <%= collection_select(:client, :name, @clients, :id, :name) %> > > If I select an option from the list, but then miss out one of my other > validation requirements the list gets reset when the form renders with > the errors. I''m sure this is because of my create action which has the > code > > def create > @image = Image.new(params[:image]) > @categories = Category.find_all > @clients = Client.find_all > > ...[snip] > end > > However if I dont recreate the variables I get errors. > > Basically what I asking is can you keep your selected option when you > re-render your form with errors? > > Any help/advice would be great. > > Many Thanks > > Kieran >
Kieran Johnson wrote:> I have a select list which is filled with options from the database > using the following code > > <%= collection_select(:client, :name, @clients, :id, :name) %> > > If I select an option from the list, but then miss out one of my other > validation requirements the list gets reset when the form renders with > the errors. I''m sure this is because of my create action which has the > code > > def create > @image = Image.new(params[:image]) > @categories = Category.find_all > @clients = Client.find_all > > ...[snip] > end > > However if I dont recreate the variables I get errors. > > Basically what I asking is can you keep your selected option when you > re-render your form with errors? >This is guess work and may be completely invalid so apologies in advance if that''s the case. If you are not already, in your create method, create a @client from the id of the selected client from the list. @client = Client.find(params[:client][:name]) My assumption is that if we have a @client available in the view, the collection_select will match the details to the relevant client from @clients. If you are already creating a @client in your create method, can you show me what it looks like please? Chris
Does this work?
<%= select ''client'', ''name'',
@client_options, {}, {} %>
..and in the controller, set @client_options to be an array of arrays, 
via something like:
@client_options = @clients.collect {|client| [client.id, client.name]}
Kieran Johnson wrote:> Sorry to bug, but this one''s really got me stuck...
> 
> On 7/19/05, Kieran Johnson
<endlessinfinity-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> 
>>I have a select list which is filled with options from the database
>>using the following code
>>
>><%= collection_select(:client, :name, @clients, :id, :name) %>
>>
>>If I select an option from the list, but then miss out one of my other
>>validation requirements the list gets reset when the form renders with
>>the errors. I''m sure this is because of my create action which
has the
>>code
>>
>>          def create
>>            @image = Image.new(params[:image])
>>            @categories = Category.find_all
>>            @clients = Client.find_all
>>
>>           ...[snip]
>>          end
>>
>>However if I dont recreate the variables I get errors.
>>
>>Basically what I asking is can you keep your selected option when you
>>re-render your form with errors?
>>