I'm trying to create simple dropdown mutiple dropdown from existing data I have model called dealers and it has city and state column , I'm trying to populate list of cities from state select from same through query. The issue is the select_tag :city is not holding the value, and when I hit continue button I'd want to post the value to my index in dealers controller. Can someone please correct me where I'm doing wrong, why isn't select_tag(:city) not holding the value it just resets to select city
class WelcomeController < ApplicationController
def index
@states= Dealer.select(:state).uniq.order('state ')
end
end
class DealersController < ApplicationController
def index
@dealers = Dealer.dealers_by_city(params[:city])
end
def update_city_select
@cities= Dealer.select(:city).uniq.where("state=?",params[:state])
render :partial => "cities", :locals => { :cities => @cities }
end
end
---View---
<%= form_tag "/dealers/index",:method => 'post',:id => 'dealer_search',:remote=> true do %>
<%= select_tag(:state,options_for_select(@states.collect{|u|[u.state,u.state]}),
{prompt: "Select State",class: "span3 input-md main_select"})%><br>
<div id="selectcities">
<%= render :partial => 'dealers/cities' %>
<%= submit_tag "Continue",:class=>"btn btn-xlarge btn-purple"%>
<% end %>
<script>
jQuery(function($){
$("#dealer_search").change(function() {
var state = $('select#state :selected').val();
if(state == "") state="0";
jQuery.get('dealers/update_city_select/'+ state, function(data){
$("#selectcities").html(data);
})
return false;
})
})
</script>
<%= form_tag cityselect_path(:state),:method => 'post',:remote=> true do %>
<% if !@cities.blank?%>
<%= select_tag(:city,
options_for_select(@cities.collect{|u| [u.city,u.city]}),
{prompt: "Select City",class: "span3 input-md main_select"})%>
<% end %>
<% end %>
--Routes--
get 'dealers/update_city_select/:state',
:controller=>'dealers', :action => 'update_city_select', as: :cityselect
Aucun commentaire:
Enregistrer un commentaire