[Rails] Re: Using AJAX to generate complex forms interactively - any guides?
On Wed, Oct 7, 2009 at 5:29 PM, ct9a <anexiole@gmail.com> wrote:
>
> thank you. Just had a look at it.
>
> Whenever you load data into objects in the controller (which gets
> received by the view), how do you stop the first element from being
> selected by default when you load the page?
>
> In relation to the example in http://pullmonkey.com/2008/3/30/dynamic-select-boxes-ruby-on-rails/,
> assume @artists has the following values: "Jimi Hendrix", "Prince",
> "Kenny Wayne Shepherd" and "Jonny Lang".
>
> Now, when the index.html.erb page loads and builds the form, it's the
> case that the first entry, "Jimi Hendrix" will be preselected.
> How do you stop that? Would you add an "empty" /nil element? Sorry,
> I'm from the Perl world and am getting myself into Rails so I might
> not know the syntax of how to do certain things.
>
> Thank you
I do something like this in my controller:
part_numbers = Part.all(:order => "number")
@parts = [["None", nil]] + part_numbers.map {|p| p.number}
and something like this in my view:
<%= select_tag "clone_part",
options_for_select(@parts, @clone_part),
:onchange => "#{remote_function(:url => new_component_path,
:with => "'clone_part='+$('clone_part').value")}"
%>
The first item in my list of part numbers is now "None". By giving it
a value of nil, this parameter will not show up in my form if the user
does not select a part. If the user does select a part, it gets
passed to my controller as a parameter to the #new_component_path
action.
--wpd
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home