Ruby on rails RoR控制器返回404错误
你好,这是我的问题 所有控制器在浏览器中始终返回404错误,但在日志中:Ruby on rails RoR控制器返回404错误,ruby-on-rails,ruby,Ruby On Rails,Ruby,你好,这是我的问题 所有控制器在浏览器中始终返回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/] 和文件/公共加载罚款。My routes.rb: map.connect ':controller/:action
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/]
和文件/公共加载罚款。My routes.rb:
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
希望您的帮助。您的路线意味着您需要始终匹配以下
/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)'
让控制器工作的一个简单方法是在路由中添加资源。这将映射所有控制器方法
# 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
如果您使用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...
在这种情况下,视图文件中的链接将是
<%= link_to "whatever_you_like_to_name", whatever_you_want_path %>
现在我有:
ActionController::RoutingError(没有路由匹配)/post/index“
我也尝试过:map.connect'post/index',:controller=>'post',:action=>'index'
但浏览器中的404和日志中的200是一样的。
# 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 %>