如何在rails中导航和读取URL?

时间:2013-01-20 19:40:36

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

这部分问题是此问题Previous Question

的一部分

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

这是结果,我相信它的错误也是免费的 -

Correct

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

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

3 个答案:

答案 0 :(得分:1)

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

但请注意

/user

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

/users

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

只需在浏览器中打开网址即表示使用获取请求,并且没有为/users设置获取路由。

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

我建议您阅读rails guide on routing

答案 1 :(得分:0)

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

对于控制器来说,

http://localhost:3000/users通常是可以的,但看起来你正在使用一些用户管理功能,所以它不接受获取请求。

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

答案 2 :(得分:0)

查看上一个问题,您还没有设置/用户。尝试添加

resources :users

到你的routes.rb。

至于Devise路线,请尝试访问http:// localhost:3000 / users / sign_in]

修改

也是这一行,

/:controller/:action/:id

不是字面意思。

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

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

这将/ users / 1与UsersController中的show方法匹配。