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


On Dec 20, 2:23 pm, Pra Ng <rails-mailing-l...@andreas-s.net> wrote:
> 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
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to