Search code examples
ruby-on-railsruby-on-rails-3gmaps4rails

route - link_to url_path


Use the google-maps-for-rails and it works great!

Can I use (routes) url_path in the marker infowindow.

I use this code now

def gmaps4rails_infowindow
    "<a href='http://domainname/en/guide/umbrie/steden/#{slug}'>#{name} - #{city_h1} </a> <br><br>#{city_description}  <br> <br> <img src='/assets/#{slug}.jpg'>  " 
end 

But I want to show another model in the same map, so different url's. I tried my url_path in the method, but with no succes. How can I do this?

Based on your reply I tried this

controller:

@json = City.all.to_gmaps4rails do |city, marker|
              marker.infowindow render_to_string(:partial => "/cities/show", :locals => { :object => city })
              marker.title   "#{city.city_h1}"
              marker.sidebar "#{city.name} - #{city.city_h1} " 
              marker.json({ :id => city.id, :name => city.name})
        end

partial:

%h1
  = city.name

i get this error message:

City Load (2.2ms)  SELECT `cities`.* FROM `cities` 
  Rendered cities/_show.html.haml (28143.9ms)
Completed 500 Internal Server Error in 28230ms

ActionView::Template::Error (undefined local variable or method `city' for #<#<Class:0x007fcab6bf0f08>:0x007fcab6be1080>):
    1: %h1
    2:   = city.name

Solution

  • Frankly you'd better use partials and declare infowindows in controller as described here.

    But well, if you really want to use url_helpers in model, add this in your model:

    delegate :url_helpers, to: 'Rails.application.routes'
    

    and use it this way in any method:

    url_helpers.root_path
    url_helpers.whatever_path