Forum: Ruby on Rails observe_field question

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.
67ec784794c54cef0cae08b1ba732685?d=identicon&s=25 Keebler None (keebler)
on 2006-03-01 08:17
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" %>

<h1>New model</h1>

<%= 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' %>

<!--[form:model]-->
<p><b><label for="model_name">Name</label></b><br/>
<%= text_field 'model', 'name'  %></p>

<p><b><label
for="model_connectionpoints">Connectionpoints</label></b><br/>
<%= text_field 'model', 'connectionpoints'  %></p>

<p><b><label for="model_switchtype"
id="model_switchtype">Type</label></b><br />
<%= radio_button("models", "switchtype", "1") %> Fixed
<%= radio_button("models", "switchtype", "2") %> Modular
<%= radio_button("models", "switchtype", "3") %> Blade</p>

<p><b><label for="model_notes">Notes</label></b><br/>
<%= text_area 'model', 'notes'  %></p>
<!--[eoform:model]-->

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.
F8bd526cf2606b05f97478f36d14e90b?d=identicon&s=25 Eleudson Queiroz (eleudson)
on 2006-04-06 02:42
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 <div> 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:

<!--[form:city]-->
<p><label for="city_state_country_id">País</label><br/>

<select id="city_state_country_id" name="city.state[country_id]">
  <% if @is_new %>
    <option selected value="0">Selecione um País</option>
    <% @form_country_id = 0 %>
  <% else%>
    <% @form_country_id = @city.state["country_id"] %>
  <% end%>
  <%= options_from_collection_for_select @countries, "id", "nome",
@form_country_id %>
</select></p>

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

<p><label for="city_state_id">Estado</label><br/></p>
<div id="select_state"></div>
        .
        .
        .


==== 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

<select id="city_state_id" name="city[state_id]">
  <%= options_from_collection_for_select @states, "id", "nome",
@form_country_id %>
</select>


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

Eleudson
jinfo@mandic.com.br


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" %>
>
> <h1>New model</h1>
>
> <%= 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' %>
>
> <!--[form:model]-->
> <p><b><label for="model_name">Name</label></b><br/>
> <%= text_field 'model', 'name'  %></p>
>
> <p><b><label
> for="model_connectionpoints">Connectionpoints</label></b><br/>
> <%= text_field 'model', 'connectionpoints'  %></p>
>
> <p><b><label for="model_switchtype"
> id="model_switchtype">Type</label></b><br />
> <%= radio_button("models", "switchtype", "1") %> Fixed
> <%= radio_button("models", "switchtype", "2") %> Modular
> <%= radio_button("models", "switchtype", "3") %> Blade</p>
>
> <p><b><label for="model_notes">Notes</label></b><br/>
> <%= text_area 'model', 'notes'  %></p>
> <!--[eoform:model]-->
>
> 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.
This topic is locked and can not be replied to.