【问题标题】:How to navigate and read URL in rails?如何在 Rails 中导航和阅读 URL?
【发布时间】:2013-01-20 19:40:35
【问题描述】:

这部分问题是这个问题的一部分Previous Question

在运行rake routes 命令并了解错误分类后,我终于想出了这个窗口,但作为 ROR 的新手,我无法追踪 URL 以产生效果。

这是结果,我相信它也没有错误 -

但是我尝试了这些网址,但没有一个有效:(

http://localhost:3000/
http://localhost:3000/users
http://localhost:3000/user

Routes.rb 文件 --

Prjmgt::Application.routes.draw do
  devise_for :users

  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Sample resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

  # Sample resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => 'welcome#index'
    root :to => 'home#index'


  # See how all your routes lay out with "rake routes"

  # 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)'
end

【问题讨论】:

  • 上一个问题的答案应该为您提供所需的所有信息
  • @Charles 我不这么认为(在这种情况下我可能错了)因为前一个是针对错误的..而这个是在纠正这些错误之后需要检查事情是否正确或不使用分类法从提供的屏幕截图中读取 Rails 结构。

标签: ruby-on-rails devise ruby-on-rails-3.2 rake


【解决方案1】:

您的根 URL 应该可以正常工作。

但是,请注意

/user

在您的路线中任何地方都不存在,而且

/users

只能通过发布请求访问。在设计中,这用于使用 post 方法提交表单以创建新用户。

只需在浏览器中打开一个url就意味着使用get请求,并且没有为/users设置get路由。

在您的浏览器中试用/users/sign_up,它应该会引导您进入设计的注册页面。

我建议您阅读rails guide on routing

【讨论】:

  • 即使localhost:3000在这种情况下也会抛出错误:(路由错误没有路由匹配[GET]“/”
  • 您的 routes.rb 中有类似 root to: "home#index" 的内容吗?你能告诉我们你的 routes.rb 文件的内容吗?
  • @Charles..刚刚添加了请检查
  • 你确定有一个 home 控制器,里面有一个 index 方法和一个 home/index.html.erb 视图吗?
  • 如果这仍然不起作用,我能想到的就是重新启动您的服务器(路由更改只有在您重新启动服务器后才有效)。请让我知道您的进展情况。
【解决方案2】:

http://localhost:3000/ 没有定义,即根目录。而http://localhost:3000/user 根本没有任何意义,因为控制器总是复数形式。

http://localhost:3000/users 对于控制器来说通常没问题,但您似乎正在使用一些用户管理的东西,所以它不接受获取请求。

试试:http://localhost:3000/users/sign_uphttp://localhost:3000/users/sign_in

【讨论】:

  • 他正在使用一个名为 devise 的“用户管理工具”(顺便说一句,这非常棒)
【解决方案3】:

查看您之前的问题,您还没有设置 /users。尝试添加

resources :users

到你的 routes.rb。

至于设计路线尝试访问 http://localhost:3000/users/sign_in]

编辑:

还有这一行,

/:controller/:action/:id

不是字面意思。

这只是 match 方法的一个例子。它应该看起来像

match "/users/:id" => "users#show"

这会将 /users/1 匹配到您的 UsersController 中的 show 方法。

【讨论】:

  • 不需要。在他的 routes.rb 文件中会有类似devise_for :users 的东西,它设置了所有需要的路由。
  • 只设置设计路线。它不设置基本的 CRUD 路由。这就是为什么它没有出现在他的 rake 路线中。
猜你喜欢
  • 2022-08-08
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多