我刚开始学习RoR。这是我的/app/trip_controller.rb文件
class TripController < ApplicationController
layout 'main'
def index
end
def map
session[:location] = Location.new(params[:location])
@map = GMap.new("map_div")
@map.control_init(:large_map => true, :map_type => true)
@map.icon_global_init( GIcon.new(:image => "http://www.google.com/
mapfiles/ms/icons/blue-pushpin.png",
:shadow => "http://www.google.com/mapfiles/shadow50.png",
:icon_size => GSize.new(32,32),
:shadow_size => GSize.new(37,32),
:icon_anchor => GPoint.new(9,32),
:info_window_anchor => GPoint.new(9,2),
:info_shadow_anchor => GPoint.new(18,25)),
"icon_source")
icon_source = Variable.new("icon_source")
source = GMarker.new([session[:location].lat,
session[:location].long],
:title => 'Source',
:info_window => "Start here!",
:icon => icon_source)
@map.overlay_init(source)
@map.center_zoom_init([session[:location].lat,
session[:location].long], 12)
@location = session[:location].location
end
端
这是我的routes.rb文件
Project::Application.routes.draw do
end
我的/ app / views文件夹包含以下文件
_search.rhtml
index.rhtml
/layouts
application.html.erb main.rhtml
/trip
_info.rhtml _tabs.rhtml map.rhtml
当我启动服务器并在浏览器上访问localhost:3000 / trip时,我收到错误
No route matches [GET] "/trip"
我是否需要在routes.rb文件中配置任何内容?
答案 0 :(得分:2)
您没有定义路线。将您的routes.rb更改为:
Project::Application.routes.draw do
get "trip" => "trip#map"
end
路由的目的是将URL请求映射到您的控制器操作,并且由于您没有定义任何路由,因此会出现路由错误。
有关路由的详细信息,请查看此guide。
可用于调试路由错误的最有用的命令可能是rake routes
,它将输出所有已定义路由的路径和名称。
答案 1 :(得分:1)
我建议通过the routing guide进行一次旅行,这可以帮助回答大多数问题,如果不是所有提出Rails新手并通过路径路由的人的问题。
要回答您的问题,是的,您需要在routes.rb
文件中配置内容 - 此文件对于完成路由工作至关重要。