Forum: Ruby on Rails dynamic drop down list submission problem

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.
Pra N. (Guest)
on 2008-12-18 15:22
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
pepe (Guest)
on 2008-12-19 06:18
(Received via mailing list)
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 <div> tag will not be considered 'input' data.
You could try to replace the <div> with something like <
%= :chapter.field_text :container... %>

Pepe
Pra N. (Guest)
on 2008-12-20 21:09
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 <div> tag will not be considered 'input' data.
> You could try to replace the <div> 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 <div> tag.
I tried to replace the <div> 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
Pra N. (Guest)
on 2008-12-20 21:23
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 <div> tag will not be considered 'input' data.
>> You could try to replace the <div> 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 <div> tag.
> I tried to replace the <div> 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 <input id="chapter_container"
name="chapter_container">.
It has no error msg, but the "input" tag wouldnt get replaced as I
wanted it to be. When I am using the <div> 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
pepe (Guest)
on 2008-12-23 03:02
(Received via mailing list)
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
pepe (Guest)
on 2008-12-24 05:08
(Received via mailing list)
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', '<option>one</
option><option>two</option><option>three</option>' %>

<div id='to_be_updated'>
  <%= render :partial => 'input_field' %>
</div>

<%= 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 <div> tags will be replaced every time you
chose a value from the select list.
3. My view initially displays a partial between the <div> 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
<div> 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
This topic is locked and can not be replied to.