2013-02-01 64 views
0

你好,這是我的問題。RoR控制器返回404錯誤

所有控制器瀏覽器總是404返回錯誤,但在日誌中:在/公共負載罰款

Processing PostController#index (for myip at 2013-02-01 13:33:02) [GET] 
Rendering post/index 
Completed in 2ms (View: 1, DB: 0) | 200 OK [http://site.com/] 

和文件。我的routes.rb:

map.connect ':controller/:action/:id' 
map.connect ':controller/:action/:id.:format' 

希望你的幫助。

回答

1

您的路線意味着您需要始終匹配以下/controller/action/id/controller/action/id.format。你應該像使用新的rails項目一樣使用括號。 注意到評論的,爲什麼你不應該這樣做

# This is a legacy wild controller route that's not recommended for RESTful applications. 
# Note: This route will make all actions in every controller accessible via GET requests. 
# match ':controller(/:action(/:id))(.:format)' 
+0

現在我有:'的ActionController :: RoutingError(無路由匹配「/後/ index'' 另外我試過這個:'map.connect'post/index',:controller =>'post',:action =>'index'',但是在瀏覽器中是404而在日誌中是200。 – droptheplot

+0

好吧,不知道爲什麼,但現在它工作。謝謝! – droptheplot

0

一個簡單的辦法讓你的控制器的工作是在你的路由添加資源。這將映射您的所有控制器方法。

# app/controllers/controllernames_controller.rb 
class ControllernamesController < ApplicationController 
    def index 
    end 

    # and other methods you want... 
end 

# config/routes.rb 
MyApp::Application.routes.draw do 
    resources :controllername 
end 

那麼你的鏈接將成爲http://localhost/controllernames/methodname/id

然後在視圖文件,你可以添加鏈接的方式如下:

<%= link_to "whatever_your_like_to_name", controllernames_path %> 

如果使用REST風格的腳手架Rails提供那麼你可以做:

<%= link_to "whatever_your_like_to_name", new_controllername_path %> 
# or 
<%= link_to "whatever_your_like_to_name", edit_controllername_path(controllername) %> 
# or 
etc... 

另一種從cont獲得單一方法的方法滾你可以做以下的路線:

# config/routes.rb 
MyApp::Application.routes.draw do 
    get 'controllernames/methodname', to: 'controllernames#methodname', as: "whatever_you_want" 
end 

在這種情況下,你在視圖文件鏈接將

<%= link_to "whatever_you_like_to_name", whatever_you_want_path %>