i want site visitors able view nearby shows within radius can input via dropdown form. have view displays nearby shows using geocoder gem:
<h3> shows near <%= request.location.city %> </h3> <%= form_for @nearby_shows.first |f| %> <p> radius (in miles): <%= f.select(:radii, [10,20,30,40,50], {}, :style => "width:50px", :selected => f.object.radii, :onchange => "location.href = '#{shows_path}'") %> </p> <% end %> <ul class="users"> <% @nearby_shows.each |nearby_show| %> <li> <%= link_to nearby_show.show_name, nearby_show %> </li> <% end %> </ul> right selection doesn't affect anything, , selection isn't remembered in form when page refreshes.
the model, show.rb contains:
attr_accessor :radii and shows controller contains:
def index @shows = show.all @radii = 50 @nearby_venues = venue.near("boulder, co",@radii,:select => "id").to_a @nearby_shows = show.where(show_venue: @nearby_venues) end in production, i'll using request.location.city, in development i'm using "boulder, co" example.
how can set @radii using input select form? concerned form_for not permit me change variable list of entities @nearby_shows.
if want fast ajax solution, here's do
first, add id list it's easy manipulate
<ul id="my_id" class="users"></ul> i don't understand why need <%= form_for @nearby_shows.first %> ? if understand well, want show select, , update list of nearby shows based on user selects ?
routes.rb
resource :shows 'update_nearby', on: :collection, constraints: { format: 'js' } end # /shows/update_nearby, meant used ajax your_view.html.erb
<%= form_tag update_nearby_shows_path, remote: :true |f| %> <p> radius (in miles): <%= select_tag(:radii, [10,20,30,40,50], {}, :style => "width:50px", :selected => @radii, :onchange => "location.href = '#{shows_path}'") %> </p> <% end %> <!-- on submit, request js response can add js submit form everytime select changes --> add js specific respone
your_controller.rb
def update_nearby find_nearby_shows end private def find_nearby_shows @radii = params[:radii] ? params[:radii] : 50 @nearby_venues = venue.near("boulder, co",@radii,:select => "id").to_a @nearby_shows = show.where(show_venue: @nearby_venues) end update_nearby.js.erb
<% if @nearby_shows %> // empty list var id = $("#my_id").empty() <% @nearby_shows.each %> // add 1 <li> per show $("<li>", { 'html': "<%= escape_javascript(link_to(nearby_show.show_name, nearby_show)) %>"}).appendto(id) <% end %> <% end %> bonus : said wanted save radius ? can try add user session
def index ... @radii = session[:saved_radii] ? session[:saved_radii] : default_radius ... end def update_nearby find_nearby_shows session[:saved_radii] = @radii end but if want save user, should have preferred_radii field in user model
Comments
Post a Comment