Friday, 27 September 2013

How can i show information by relationship using ajax?

How can i show information by relationship using ajax?

How can i create an ajax in my search when i select a country it would
show me all states that i have in the country selected
My tables
Countries
|id| |country_name|
1 'USA'
2 'Peru'
States
|id| |state_name|
1 Alabama
2 Machupicchu
Country_States
|id| |country_id| |state_id|
1 1 1
2 2 2
My controller
class Country_StatesController < ApplicationController
def conditional
@countries = Country.find(:all)
@states= State.find(:all)
@selected_country = Country.find_by_id(params[:countries]) if
params[:countries].to_i
@selected_state = State.find_by_id(params[:states]) if
params[:states].to_i
@search= CountryState.find(:all,:conditions=> ['state_id','country_id'
],params[:states],params[:countries] )
end
end
My view
<% form_tag :controller=>"country_States",:action=>"conditional" do %>
<%= select_tag "countries", options_for_select(@states.collect {|t|
[t.state_name.to_s ,t.id]}) %>
<%= select_tag "states", options_for_select(@states.collect {|t|
[t.state_name.to_s ,t.id]}, params[:search].to_i ) %>
<%= submit_tag "Search", :name => nil %>
<% end %>
I will appreciate help.

No comments:

Post a Comment