Observe_field question


#1

I’m sure this is a simple problem, but I’ve been working on it for a
couple days and can’t seem to figure out the correct way to structure my
observe_field call. The HTML and Javascript generated all appear
correct, but the AJAX call is never made. Below is the code I’m using
and any help would be much appreciated.

models_controller.rb

def display_compatible
render :partial => “compatible”
end

( Right now the _compatible.rhtml file contains a single HTML statement
just for testing. Going to localhost:3000/app/models/display_compatible
displays the test statement correctly. )

views/new.rhtml

<%= javascript_include_tag “prototype” %>

New model

<%= start_form_tag :action => ‘create’ %>
<%= render_partial ‘form’ %>
<%= submit_tag “Create” %>
<%= end_form_tag %>

<%= observe_field(“model_switchtype”,
:frequency => 0.5,
:update => :compatible,
:url => { :action => :display_compatible }) %>

<%= link_to ‘Back’, :action => ‘list’ %>

( Still using the basic scaffold generated code at the moment. )

views/_form.rhtml

<%= error_messages_for ‘model’ %>

Name
<%= text_field 'model', 'name' %>

Connectionpoints
<%= text_field 'model', 'connectionpoints' %>

Type
<%= radio_button("models", "switchtype", "1") %> Fixed <%= radio_button("models", "switchtype", "2") %> Modular <%= radio_button("models", "switchtype", "3") %> Blade

Notes
<%= text_area 'model', 'notes' %>

What I’m trying to do is monitor the radio button field for a change,
and on a change display another set of entry boxes, but am pretty well
stuck.

Thanks in advance.


#2

Dear Keebler,

  • the <%= javascript_include_tag “prototype” %> I include in
    application.rhtml
  • the observe_fiel I include in the _form.rhtml
  • In the _form.rhtml I create the tag
    with name of the :update
    parameter from observe_field
  • and create the display_compatible.rhtml with the html code of the
    field that will be brought up to date
  • It sees the code below:

    País
    <% if @is_new %> Selecione um País <% @form_country_id = 0 %> <% else%> <% @form_country_id = @city.state["country_id"] %> <% end%> <%= options_from_collection_for_select @countries, "id", "nome", @form_country_id %>

    <%= observe_field(:city_state_country_id,
    :update => :select_state,
    :url => {:action => :ajax_states_from_country}
    )
    %>

    Estado

    . . .

    ==== city_controller.rb

    def ajax_states_from_country
    @states = State.find(:all, :order => “nome”, :conditions =>
    [“country_id = ?”, request.raw_post])
    render(:layout => false)
    end

    ==== ajax_states_from_country.rhtml

    <%= options_from_collection_for_select @states, "id", "nome", @form_country_id %>

    I wait to have helped it, therefore also I am with doubts and includes
    the topic “3 doubts on observe_field”

    Eleudson
    removed_email_address@domain.invalid

    Keebler none wrote:

    I’m sure this is a simple problem, but I’ve been working on it for a
    couple days and can’t seem to figure out the correct way to structure my
    observe_field call. The HTML and Javascript generated all appear
    correct, but the AJAX call is never made. Below is the code I’m using
    and any help would be much appreciated.

    models_controller.rb

    def display_compatible
    render :partial => “compatible”
    end

    ( Right now the _compatible.rhtml file contains a single HTML statement
    just for testing. Going to localhost:3000/app/models/display_compatible
    displays the test statement correctly. )

    views/new.rhtml

    <%= javascript_include_tag “prototype” %>

    New model

    <%= start_form_tag :action => ‘create’ %>
    <%= render_partial ‘form’ %>
    <%= submit_tag “Create” %>
    <%= end_form_tag %>

    <%= observe_field(“model_switchtype”,
    :frequency => 0.5,
    :update => :compatible,
    :url => { :action => :display_compatible }) %>

    <%= link_to ‘Back’, :action => ‘list’ %>

    ( Still using the basic scaffold generated code at the moment. )

    views/_form.rhtml

    <%= error_messages_for ‘model’ %>

    Name
    <%= text_field 'model', 'name' %>

    Connectionpoints
    <%= text_field 'model', 'connectionpoints' %>

    Type
    <%= radio_button("models", "switchtype", "1") %> Fixed <%= radio_button("models", "switchtype", "2") %> Modular <%= radio_button("models", "switchtype", "3") %> Blade

    Notes
    <%= text_area 'model', 'notes' %>

    What I’m trying to do is monitor the radio button field for a change,
    and on a change display another set of entry boxes, but am pretty well
    stuck.

    Thanks in advance.