简体   繁体   English

Rails 4 路由错误“没有路由匹配 [GET]”/project”

[英]Rails 4 routing error 'No route matches [GET] "/project"

I moved my rails project to another computer and I getting this error:我将 Rails 项目移到另一台计算机上,但出现此错误:

No route matches [GET] "/project"

Rails.root: /home/user/project

I already trying everything and still doesn't work.我已经尝试了一切,但仍然无法正常工作。

My routes.rb is the following:我的 routes.rb 如下:

Rails.application.routes.draw do

  resources :orders
  get 'product_statuses/list'

  resources :customers do
    resources :shipping_addresses
  end

  resources :products do
    resources :product_details do
      resources :product_statuses
    end
    resources :prices
  end

  get 'welcome/index'

end

I really need help :( please.我真的需要帮助:(拜托。

Thanks!谢谢!

If you're trying to view a particular product, /product is not a route that will work because there is no way to identify it.如果您尝试查看特定产品,则 /product 不是可行的路径,因为无法识别它。 Given a product with an id of 1, the routes you have defined work like this:给定一个 id 为 1 的产品,您定义的路由的工作方式如下:

/products/1
/products/1/edit

You can get rid of the /products/ segment by providing a blank path on the resource, although there are things to be aware of when occupying the root namespace like this:您可以通过在资源上提供一个空白路径来摆脱 /products/ 段,尽管在占用根命名空间时需要注意如下事项:

resources :products, path: '' do
  ...
end

This will give you routes like this:这将为您提供如下路线:

/1
/1/edit

If you aren't happy with the id being displayed in the url, you can either manually write new routes like this (assuming a product has a name attribute):如果您对 url 中显示的 id 不满意,您可以手动编写这样的新路由(假设产品具有 name 属性):

get '/:name', to: 'products#show'

Just be aware that this won't work unless you're enforcing uniqueness on the name attribute.请注意,除非您在 name 属性上强制执行唯一性,否则这将不起作用。

Another options is to can override the param that is displayed and use the routes you already have:另一个选项是可以覆盖显示的参数并使用您已有的路由:

class Product

  def to_param
    name.parameterize
  end

  ...
end

This will give you routes like this:这将为您提供如下路线:

/products/foo
/products/darth-vader-marshmallows

It would be worth your while to review the Rails routing documentation here: http://guides.rubyonrails.org/routing.html值得您花时间在这里查看 Rails 路由文档: http : //guides.rubyonrails.org/routing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM