[英]Rails routing issues and questions
I have been trying to read the rails routing from outside in and failing miserably. 我一直在尝试从外部读取Rails路由,但失败了。 I believe what I want to do is pretty basic, but I for whatever reason can't figure it out. 我相信我想做的事很基础,但是出于任何原因我都无法弄清楚。 Here is the situation. 这是情况。
I want my urls to read www.example.com/maps instead of what they are doing now www.example.com/maps/map which is the controller then view names. 我希望自己的网址阅读www.example.com/maps,而不是它们现在正在做什么,而www.example.com/maps/map是控制器,然后查看名称。
My routes look like the following. 我的路线如下所示。
homepages GET /homepages(.:format) homepages#index
POST /homepages(.:format) homepages#create
new_homepage GET /homepages/new(.:format) homepages#new
edit_homepage GET /homepages/:id/edit(.:format) homepages#edit
homepage GET /homepages/:id(.:format) homepages#show
PUT /homepages/:id(.:format) homepages#update
DELETE /homepages/:id(.:format) homepages#destroy
root / homepages#index
maps GET /maps(.:format) maps#index
POST /maps(.:format) maps#create
new_map GET /maps/new(.:format) maps#new
edit_map GET /maps/:id/edit(.:format) maps#edit
map GET /maps/:id(.:format) maps#show
PUT /maps/:id(.:format) maps#update
DELETE /maps/:id(.:format) maps#destroy
work_index GET /work(.:format) work#index
POST /work(.:format) work#create
new_work GET /work/new(.:format) work#new
edit_work GET /work/:id/edit(.:format) work#edit
work GET /work/:id(.:format) work#show
PUT /work/:id(.:format) work#update
DELETE /work/:id(.:format) work#destroy
contact_index GET /contact(.:format) contact#index
I tried doing something like resources :maps, :path => '' but when I visited my site the url appeared the same. 我尝试做诸如资源:maps,:path =>''之类的事情,但是当我访问我的网站时,URL看起来是一样的。 Is that due to the fact the code on my view looks like 是因为我视图上的代码看起来像
<li class="current_page_item"><%= link_to "Bio", :controller => :homepages, :action => :index %></li>
<li><%= link_to "Work", :controller => :work, :action => :experience %></li>
<li><%= link_to "Map", :controller => :maps, :action => :map %></li>
<li><%= link_to "Contact", :controller => :contact, :action => :email %></li>
Or is that because my routes are wrong? 还是因为我的路线错误?
My route file currently looks like 我的路线文件当前看起来像
Me::Application.routes.draw do
resources :homepages
root :to => 'homepages#index'
resources :maps
resources :work
resources :contact
resources :media
Thanks for the assistance! 感谢您的协助!
It seems to me that you should read a little but about Routes and their part in the grand scheme of Rails 在我看来,您应该阅读一些有关Routes及其在Rails宏伟计划中的角色的信息。
You can start here http://guides.rubyonrails.org/routing.html 您可以从这里开始http://guides.rubyonrails.org/routing.html
You can also look at some RailCast in order to fully grasp the meaning of routes It is a little bit tricky , but when you understand it it fully make sense 您也可以查看一些RailCast以便完全掌握路线的含义。这有点棘手,但是当您理解它的时候就很有意义了。
If you want the "Map" link to go to a specific map you should change the 如果您希望“地图”链接转到特定地图,则应更改
<%= link_to "Map", :controller => :maps, :action => :map %>
To 至
<%= link_to "Map", @map %>
Where @map is the variable of that map that you initialized in the controller 其中@map是您在控制器中初始化的那个映射的变量
If you want the "Map" link to go to the maps index page and display all the maps you should change the 如果您希望“地图”链接转到地图索引页面并显示所有地图,则应更改
<%= link_to "Map", :controller => :maps, :action => :map %>
To 至
<%= link_to "Map", maps_path %>
Well this is how REST resource and URL are defined. 好的,这就是REST资源和URL的定义方式。 Read here 在这里阅读
When doing the following in your routes.rb
file: 在routes.rb
文件中执行以下操作时:
resource :maps
You are telling rails to generate default routes for you for the following actions: 您正在告诉Rails为以下操作生成默认路由:
GET /maps
索引=> GET /maps
GET /maps/1
显示=> GET /maps/1
GET /maps/new
新=> GET /maps/new
POST /maps
创建=> POST /maps
GET /maps/1
编辑=> GET /maps/1
PUT /maps/1
更新=> PUT /maps/1
DELETE /maps/1
销毁=> DELETE /maps/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.