【问题标题】:Ruby on Rails undefined method `products_path' routing issueRuby on Rails 未定义方法“products_path”路由问题
【发布时间】:2013-06-01 08:59:27
【问题描述】:

自从我在 Rails 工作以来已经有一段时间了,我在正确设置我的路线时遇到了一些问题。任何帮助将不胜感激。

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

产品.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

比赛.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

产品.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

出价.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

标签.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

【问题讨论】:

  • 运行 rake routes 以查看所有路线。实际上,路由应该像 admin_products_path 因为你有 :admin 命名空间
  • Sergey,运行 rake 路由给了我以下信息:new_admin_product GET /admin/products/new(.:format) admin/products#new
  • 如果你看到你有admin_products。这就是你需要的。试试&lt;%= link_to "Admin products", admin_products_path %&gt;
  • 哦,我知道怎么了。我会发布一个答案。

标签: ruby-on-rails ruby-on-rails-3.2 routes undefined nested-routes


【解决方案1】:

由于您在 URL: http://localhost:3000/admin/products/new 处遇到错误,因此您的表单构建器应该有一些问题。

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

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

而不仅仅是

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

【讨论】:

  • 哇。现在我什至不知道。我不能感谢你!我也对答案表示赞同,以便在应得的地方给予赞扬。再次感谢!!!!
  • 你可能还需要, url: admin_product_path(resource)
【解决方案2】:

您在新模板中的某个位置调用products_path。由于它是在管理员之下,你应该把它改成admin_products_path

【讨论】:

  • 嗨 Josh,我在 admin/dashboard/index.html.haml 中有这个:= link_to "Add Product", new_admin_product_path(@product)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多