您好,这是我的问题。
所有控制器在浏览器中始终返回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/]
/ public中的文件加载正常。我的routes.rb:
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
希望得到你的帮助。
答案 0 :(得分: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)'
答案 1 :(得分: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 %>
如果您使用Rails提供的RESTful脚手架,则可以执行以下操作:
<%= link_to "whatever_your_like_to_name", new_controllername_path %>
# or
<%= link_to "whatever_your_like_to_name", edit_controllername_path(controllername) %>
# or
etc...
从控制器获取单个方法的另一种方法是,您可以执行以下路线:
# 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 %>