0

我已经在Rails工作了一段时间,并且在正确设置路由时遇到了一些问题。任何帮助将不胜感激。Ruby on Rails未定义的方法`products_path'路由问题

URL: http://localhost:3000/admin/products/new 

Error: undefined method `products_path' for #<#<Class:0x007f9f569d0150>:0x007f9f578ccb18> 

耙路由

admin_products  GET /admin/products(.:format)   admin/products#index 
        POST /admin/products(.:format)   admin/products#create 
new_admin_product GET /admin/products/new(.:format)  admin/products#new 
edit_admin_product GET /admin/products/:id/edit(.:format) admin/products#edit 
admin_product GET   /admin/products/:id(.:format)  admin/products#show 
        PUT /admin/products/:id(.:format)  admin/products#update 
        DELETE /admin/products/:id(.:format)  admin/products#destroy 

的routes.rb

Aneprize::Application.routes.draw do 
    devise_for :admins, :users 

    namespace :admin do 
    match '/', to: 'dashboard#index', as: '/' 

    authenticated :admin do 
     root to: 'dashboard#index', as: :root 

     resources :products do 
     resource :contest 
     resources :bids 
     resources :photos 
     resources :tags 
     end 
    end 
    end 

    root to: 'contests#index' 
end 

product.rb

class Product < ActiveRecord::Base 
    attr_accessible :name, :retail_price, :short_description, :long_description, :weight 

    has_one :contest, dependent: :destroy 
    has_many :photos, dependent: :destroy 
    has_many :bids, dependent: :destroy 
    has_many :tags 

    validates :name, :short_description, :long_description, presence: true 
    validates :retail_price, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 } 
    validates :weight, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1 } 
end 

contest.rb

class Contest < ActiveRecord::Base 
    attr_accessible :product_id, :duration 

    belongs_to :product, dependent: :destroy 

    validates :product_id, presence: true, numericality: { only_integer: true, greater_than: 0 } 
    validates :duration, presence: true, numericality: { only_integer: true, greater_than: 2 } 
end 

product.rb

class Photo < ActiveRecord::Base 
    attr_accessible :product_id, :image_url 

    belongs_to :product 

    validates :product_id, presence: true, numericality: { only_integer: true, greater_than: 0 } 
    validates :image_url, presence: true, format: { with: /^[^-\d].+/ } 
end 

bid.rb

class Bid < ActiveRecord::Base 
    attr_accessible :product_id, :account_id, :amount 

    belongs_to :account 
    belongs_to :product, dependent: :destroy 

    validates :account_id, :product_id, :amount, presence: true, numericality: { only_integer: true, greater_than: 0 } 
end 

tag.rb

class Tag < ActiveRecord::Base 
    attr_accessible :name, :product_id 

    belongs_to :product 

    validates :name, presence: true, format: { with: /^\D+$/ } 
    validates :product_id, presence: true, numericality: { only_integer: true, greater_than: 0 } 
end 
+1

运行'耙routes'看到所有路由。其实,路由应该像'admin_products_path',因为你有':admin'命名空间 –

+0

Sergey,运行耙路由给我下面: new_admin_product GET /admin/products/new(.:format)admin/products#new –

+0

如果你看到你有'admin_products'。这就是你需要的。用'<%= link_to“管理产品试试iy,”admin_products_path%>' –

回答

3

因为你在URL: http://localhost:3000/admin/products/new错误,所以你应该有一些与你的表单生成器的问题。

虽然在:admin名称空间中有产品资源,但在构建表单时应考虑到这一事实。正确的例子是:

<%= form_for [:admin, @product] do |f| %> 
    ... # whatever 
<% end %> 

,而不是仅仅

<%= form_for @product do |f| %> 
    ... # whatever 
<% end %> 
+0

哇。现在我甚至不知道。我不能够感谢你!我已经提出了答案,并在应有的地方给予奖励。再次感谢!!!! –

+0

我很乐意帮助您 –

+0

您可能还需要',url:admin_product_path(resource)'' –

0

某处在您的新模板您呼叫products_path。由于它是在管理你应该将其更改为admin_products_path

+0

嘿乔希,我已经在管理员/仪表板/ index.html.haml: = link_to“添加产品”,new_admin_product_path(@product) –