Forum: Ruby on Rails 3 doubts on observe_field

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.
F8bd526cf2606b05f97478f36d14e90b?d=identicon&s=25 Eleudson Queiroz (eleudson)
on 2006-04-06 02:26
Dear friends, with the yours aid already I obtained to use
observe_field, but still I am with some problems and necessary again.
They are three questions to long of the source code below.

Since already I am grateful for the tips.

==== _form.rhtml

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

<select id="city_state_country_id" name="city.state[country_id]">

*** QUESTION 1: How I can know if I am in the action new or edit without
needing to create the @is_new variable?

  <% 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}
	)
 %>

*** QUESTION 2: How to force the exhibition of select_state when the
page is loaded and it did not have change of value of country_id?
This operation is necessary to show the state field in the edition
action of the data.

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


==== city_controller.rb

*** QUESTION 3: How I can receive the value from @form_country_id for
parameter?
Not to need to use request.raw_post...

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

... and not to use @form_country_id variable from _form.rhtml.

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



Regards,

Eleudson Queiroz
jinfo@mandic.com.br
This topic is locked and can not be replied to.