0

I want associate a Place to an Activity, a Place has the following fields: a country a city, a adress and a name. I want to do something like, as soon as you choose a country the will only and a city, there will only appear (in a dropdown) the Places that have that belong to that city, is there anyway how i can do that? My main problem is the variable :city_name, i know i cant create variables in views like this, but i really dont know how to do it and also how to temporarily save it... Any help would be appreciated :)

This code doesn't work, its just a prototype from what i want to do:

 <%= form_for @activity, :html => { :class => "form-horizontal activity" } do |f| %>
            <div class="transbox">
          <h1><%= "Add Place To activity" %></h1>
          </div>
          <div class = "box_round">
            <div class="row">
              <div class="control-group col-lg-10">
                <%= f.label "City Name", :class => 'control-label' %>
                <div class="controls">
                  //variable :city_name does not exist
                  <%= f.text_field :city_name, :class => 'form-control' %>
                </div>
                <%= error_span(@activity[:resume]) %>
              </div>
              <% if !:city_name.nil? %>
                  <div class="control-group col-lg-10">
                    <%= f.label "City Name", :class => 'control-label' %>S
                    <div class="controls">
                      <% place_options = options_from_collection_for_select(@places.where(:city => :city_name).sort { |a,b| a.name <=> b.name }, :id, :name, selected: params[:place_id]) %>
                      <%= f.select(:place_id, place_options, {prompt: 'Select Sport'},
                                   {prompt: 'Select Place', include_blank: false, class: "Place" }) %>
                      <%= link_to "Place doesn't exist yet?" , new_place_path %>
                    </div>
                    <%= error_span(@activity[:resume]) %>
                  </div>
              <% end %>
          </div>
        </div>
        </div>
        <% end %>

1 Answer 1

1

What you are looking for is an called an AJAX request. Its a bit much to code out for you, but the gist of it is:

  1. Write JS watcher to look at country field.
  2. On country field change, make a JS call to the server and say: "Give me all the cities in that country"
  3. Return JS that takes the list of cities and overwrites the available options in the city's dropdown field.

You may choose to do the same for Places as well, but the concept/idea is the same. For reference fun, check out this Railscast: http://railscasts.com/episodes/136-jquery-ajax-revised

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.