# Dynamic drop down list submission problem

**URL:** <https://www.ruby-forum.com/t/dynamic-drop-down-list-submission-problem/155956>\
**Category:** Rails\
**Created:** [December 18, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/dynamic-drop-down-list-submission-problem/155956 "2008-12-18T14:21:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![junsha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@junsha](https://www.ruby-forum.com/u/junsha)\
**Post date:** [December 18, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/dynamic-drop-down-list-submission-problem/155956/1 "2008-12-18T14:21:59Z")

</div>

hi all,

i got a little problem here.  
with my code below, what i am trying to do is to select the value from  
drop down list, and then updating the value of the text field.  
However, after updating the value of the text field, the value of the  
text field  
is not submitted by pressing the submit button.

chapter controllers:  
def upload  
@chapter = Chapter.new  
@uploads = Book.find(:all)

def get\_chapter\_number  
@books= Book.find(:first,  
:conditions =\> [“title = ?”, params[“title”]])  
render :layout =\> false  
end  
end

views for upload:  
\<%= javascript\_include\_tag “prototype” %\>  
\<% form\_for (:chapter, @chapter, :url=\>{:action =\>  
“uploadFile”}, :html =\> {:multipart =\> true}) do |f| -%\>

```
        <label class="required">series title:</label>
        <select name="upload[title]" id="upload[title]">
        <% @uploads.each do |upload| %>
          <option value="<%= upload.title %>">
            <%= upload.title %>
          </option>
        <% end %>
        </select>

        <label class="required"> chapter: </label>
        <div id="chapter_container">
        </div>

        <%= observe_field("upload

```

[title]", :update=\>“chapter\_container”, :url=\>{:action =\>  
“get\_chapter\_number”}, :with =\> ‘title’, :on=\>“changed”) %\>

```
         <%= submit_tag 'Upload', :class => "submit" %>

```

views for get\_chapter\_number:  
\<input type=“text” id=“upload\_number\_of\_chapters” name=“upload  
[number\_of\_chapters]” value= \<%= @books.number\_of\_chapters + 1 %\> size  
= “30” /\>

appreciate any help… thanks,

prawira

---

<div class="post-metadata">

**Author:** ![junsha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@junsha](https://www.ruby-forum.com/u/junsha)\
**Post date:** [December 19, 2008, 5:18am UTC](https://www.ruby-forum.com/t/dynamic-drop-down-list-submission-problem/155956/2 "2008-12-19T05:18:50Z")

</div>

I believe you are missing an input field for your updated value. The  
submit will pass along the values of the input fields. Something you  
just ‘write’ inside a

 tag will not be considered ‘input’ data.  
You could try to replace the 
 with something like \<  
%= :chapter.field\_text :container… %\>

Pepe

---

<div class="post-metadata">

**Author:** ![junsha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@junsha](https://www.ruby-forum.com/u/junsha)\
**Post date:** [December 20, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/dynamic-drop-down-list-submission-problem/155956/3 "2008-12-20T20:09:10Z")

</div>

pepe wrote:

> I believe you are missing an input field for your updated value. The  
> submit will pass along the values of the input fields. Something you  
> just ‘write’ inside a
> 
> tag will not be considered ‘input’ data.  
> You could try to replace the 
> with something like \<  
> %= :chapter.field\_text :container… %\>
> 
> Pepe
> 
> Hi Pepe,
> 
> Thanks for your advice…  
> I still don’t get how it supposed to work though.  
> I am still not sure how to get it work by replacing the
> 
> tag.  
> I tried to replace the 
> tag with  
> \<% input type=“text”, id=“chapter\_container”, name=“chapter\_container”  
> %\>  
> but it keep giving me error “The error occurred while evaluating  
> nil.column\_for\_attribute”.  
> Any idea?
> 
> Thanks

---

<div class="post-metadata">

**Author:** ![junsha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@junsha](https://www.ruby-forum.com/u/junsha)\
**Post date:** [December 23, 2008, 2:02am UTC](https://www.ruby-forum.com/t/dynamic-drop-down-list-submission-problem/155956/4 "2008-12-23T02:02:23Z")

</div>

Hi Pra,

Sorry, I have been (still am) busy. I’ll try to give you an example  
ASAP but it might take me a little while. Hopefully soon.

Pepe

---

<div class="post-metadata">

**Author:** ![junsha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@junsha](https://www.ruby-forum.com/u/junsha)\
**Post date:** [December 20, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/dynamic-drop-down-list-submission-problem/155956/5 "2008-12-20T20:23:25Z")

</div>

Pra Ng wrote:

> pepe wrote:
> 
> > I believe you are missing an input field for your updated value. The  
> > submit will pass along the values of the input fields. Something you  
> > just ‘write’ inside a
> > 
> > tag will not be considered ‘input’ data.  
> > You could try to replace the 
> > with something like \<  
> > %= :chapter.field\_text :container… %\>
> > 
> > Pepe
> > 
> > Hi Pepe,
> > 
> > Thanks for your advice…  
> > I still don’t get how it supposed to work though.  
> > I am still not sure how to get it work by replacing the
> > 
> > tag.  
> > I tried to replace the 
> > tag with  
> > \<% input type=“text”, id=“chapter\_container”, name=“chapter\_container”  
> > %\>  
> > but it keep giving me error “The error occurred while evaluating  
> > nil.column\_for\_attribute”.  
> > Any idea?
> > 
> > Thanks
> > 
> > Hi Pepe,  
> > I have tried to use .  
> > It has no error msg, but the “input” tag wouldnt get replaced as I  
> > wanted it to be. When I am using the
> > 
> > tag, the div tag will get  
> > replaced with the  
> > function that I called which is “get\_chapter\_number”.  
> > After I replace it with “input” tag, it wouldnt get replaced.  
> > any idea on how to get it done?
> > 
> > Regards,
> > 
> > Prawira

---

<div class="post-metadata">

**Author:** ![junsha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@junsha](https://www.ruby-forum.com/u/junsha)\
**Post date:** [December 24, 2008, 4:08am UTC](https://www.ruby-forum.com/t/dynamic-drop-down-list-submission-problem/155956/6 "2008-12-24T04:08:04Z")

</div>

Hi Pra,

Sorry it took so long. Here is a full example:

This is my view (file name is ‘index.rhtml’):

\<%= javascript\_include\_tag :defaults %\>

Select one: \<%= select\_tag ‘my\_select’, ‘one\</  
option\>twothree’ %\>

 \<%= render :partial =\> 'input\_field' %\>

\<%= observe\_field ‘my\_select’,  
:frequency =\> 0.5,  
:update =\> ‘to\_be\_updated’,  
:url =\> {:action =\> ‘observe\_my\_select’},  
:with =\> ‘my\_select’ %\>

This is my partial (file name is ‘\_input\_field.rhtml’):

Output value: \<%= text\_field\_tag ‘input\_field’, @to\_be\_updated %\>

This is my controller (file name is ‘observe\_controller.rb’):

class ObserveController \< ApplicationController  
def observe\_my\_select  
@to\_be\_updated = params[:my\_select]  
render :partial =\> ‘input\_field’  
end  
end

Here is how it works:

1. In the view you have to include the ‘prototype’ Javascript library  
with \<%= javascript\_include\_tag :defaults %\>
2. In the view you have to have an element with an id value that  
you’ll use in the ‘observe\_field’ method, which will be the target of  
the partial page update. In this case I named mine ‘to\_be\_updated’.  
Whatever is between the 
 tags will be replaced every time you  
chose a value from the select list.
3. My view initially displays a partial between the 
 tags with  
the field that I will be updating with the value from the select list.  
If I didn’t render the partial when first displaying the page, the
 would be empty and the field would obviously not display. 4. In the 'observe\_field' method: 4.1. "frequency" sets the time interval that the select field should be checked for changes. Here we are checking every 1/2 second 4.2. "update" indicates the tag with the id which contents are going to be replaced. In this case it is 'to\_be\_updated', as indicated above 4.3. "url" indicates which resource (method) will be called in the controller when a change is observed in the select list. In this case the method name is 'observe\_my\_select' 4.4. "with" indicates what value must be passed to the method indicated in the url. In this case it is the value coming from the select list. 5. You need a partial with the field definition (\_input\_field.rhtml) that will be displayed every time a selection is made. You need the field in a partial because you are building it on the fly, since you need to update the field's contents (with class variable @to\_be\_updated). 6. The controller contains the method 'observe\_my\_select', which updates class variable "@to\_be\_updated" with the value sent from the Javascript generated by 'observe\_field' (:with =\> 'my\_select). 7. The controller method returns the result of calling a 'render' method with the partial containing the input field definition, that way the input field gets 'written' to the page and uses the value of @to\_be\_updated to initialize its contents.

Please let me know if you need anything else.

Pepe
