Forum: Ruby on Rails autocomplete 2nd field based on 1st field's input

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2addfb9463ae86d58614733ea16b0682?d=identicon&s=25 Ryan Williams (jimmyether)
on 2006-04-10 19:18
I'm trying to populate a field named "file_name" based on the input of a
field named "title". So, for instance, based on the following input in
the "title" field:

My Title Is Awesome

I would want the following "file_name" to be autocompeted on the fly:

My_Title_Is_Awesome

I've been going over all the autocomplete examples out there, but since
they all seem to apply only to pulling data from a model, I'm not sure
how I could approach this. Surely others have done something like this
in rails. Any suggestions?
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-10 19:41
Ryan Williams wrote:
> I'm trying to populate a field named "file_name" based on the input of a
> field named "title". So, for instance, based on the following input in
> the "title" field:
>
> My Title Is Awesome
>
> I would want the following "file_name" to be autocompeted on the fly:
>
> My_Title_Is_Awesome
>
> I've been going over all the autocomplete examples out there, but since
> they all seem to apply only to pulling data from a model, I'm not sure
> how I could approach this. Surely others have done something like this
> in rails. Any suggestions?

I'm not sure of the exact syntax of the code, but in theory what you
might want to do is use the observe_field function in your view, to
observe the title field.  You could set observe_field to call a method
in your controller which uses RJS to set the innerHTML of your file_name
field.

Look into observe_field, and I'm not sure exactly how you set the
innerHTML with RJS, but that's where I'd start.

Jeff Coleman
009c581be96c1c9a2da7da4741c9580b?d=identicon&s=25 Anonymous (Guest)
on 2006-04-29 00:57
(Received via mailing list)
You can try something like this:


<%= text_field( 'some_class', 'title' ) %>
<%= observe_field(
  'some_class_title',
  :function =>
"$('some_class_file_name').value=value.toLowerCase().replace(/\s/g,
'_')",
  :frequency =&mt; 0.5 ) %>
<%= text_field( 'some_class', 'file_name' ) %>



This should update the "file_name" field using the contents of "title"
and format the string according to whatever you want. In this case, the
"observe_field" method will work locally without Ajax calls.


In case you need a more complex logic, the function could be executed in
the controller. In order to update the second field you could have an
empty DIV or something at the bottom of the page and print from the
controller the javascript block. Should also work.


R.
2addfb9463ae86d58614733ea16b0682?d=identicon&s=25 Ryan Williams (jimmyether)
on 2006-04-29 01:21
Yes! Thanks! This did indeed work:

<pre>
<%= text_field( 'some_class', 'title' ) %>
<%= observe_field(
  'some_class_title',
  :function =>
"$('some_class_file_name').value=value.toLowerCase().replace(/\s/g,
'_')",
  :frequency => 0.5 ) %>
<%= text_field( 'some_class', 'file_name' ) %>
</pre>
This topic is locked and can not be replied to.