route - link_to url_path

时间:2012-08-21 11:12:44

标签: ruby-on-rails ruby-on-rails-3 gmaps4rails

使用google-maps-for-rails,效果很好!

我可以在标记infowindow中使用(路由)url_path。

我现在使用此代码

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 

但是我想在同一张地图中显示另一个模型,所以不同的网址。我在方法中尝试了我的url_path,但没有成功。我怎么能这样做?

根据你的回复我试过这个

控制器:

@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

部分:

%h1
  = city.name

我收到此错误消息:

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

1 个答案:

答案 0 :(得分:2)

坦率地说,你最好使用partials并在控制器中声明infowindows,如here所述。

但是,如果您真的想在模型中使用url_helpers,请在模型中添加:

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

并以任何方式使用它:

url_helpers.root_path
url_helpers.whatever_path