Ruby On Rails路由错误(以前工作)

时间:2013-08-30 14:17:29

标签: ruby-on-rails rails-routing

所以我正在开发一个项目,我想创建一个新的子类别。该链接正在运行,但我没有收到以下错误:

  

没有路线匹配{:action =>“show”,:controller =>“subategories”}

该项目相当新,所以我的路线文件非常简单:

Mrprice::Application.routes.draw do

get "admin/index" => 'admin#index'

 resources :products
 resources :subcategories
 resources :categories

 root to: 'store#index', as: 'root'

end

在我看来:

<%= link_to 'New Subcategory', new_subcategory_path %>

我确定在某个地方有拼写错误,否则解决方案非常简单,但我不能为我的生活弄清楚。 我希望我已经提供了足够的信息,而且有人在这里知道问题是什么!

编辑:subcategories_controller.rb(相关方法)

def show
@subcategory = Subcategory.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @subcategory }
end
end

def new
@subcategory = Subcategory.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @subcategory }
end
end

  def create
@subcategory = Subcategory.new(params[:subcategory])

respond_to do |format|
  if @subcategory.save
    format.html { redirect_to @subcategory, notice: 'Subcategory was successfully      created.' }
    format.json { render json: @subcategory, status: :created, location: @subcategory }
  else
    format.html { render action: "new" }
    format.json { render json: @subcategory.errors, status: :unprocessable_entity }
  end
end
end

rake路线给出:

     admin_index GET    /admin/index(.:format)            admin#index
    products GET    /products(.:format)               products#index
             POST   /products(.:format)               products#create
 new_product GET    /products/new(.:format)           products#new
edit_product GET    /products/:id/edit(.:format)      products#edit
     product GET    /products/:id(.:format)           products#show
             PUT    /products/:id(.:format)           products#update
             DELETE /products/:id(.:format)           products#destroy
subcategories GET    /subcategories(.:format)          subcategories#index
                 POST   /subcategories(.:format)          subcategories#creat
 new_subcategory GET    /subcategories/new(.:format)      subcategories#new
edit_subcategory GET    /subcategories/:id/edit(.:format) subcategories#edit
     subcategory GET    /subcategories/:id(.:format)      subcategories#show
                 PUT    /subcategories/:id(.:format)      subcategories#updat
                 DELETE /subcategories/:id(.:format)      subcategories#destr
      categories GET    /categories(.:format)             categories#index
                 POST   /categories(.:format)             categories#create
    new_category GET    /categories/new(.:format)         categories#new
   edit_category GET    /categories/:id/edit(.:format)    categories#edit
        category GET    /categories/:id(.:format)         categories#show
                 PUT    /categories/:id(.:format)         categories#update
                 DELETE /categories/:id(.:format)         categories#destroy
            root        /                                 store#index

注意:不确定如何修复格式 - 我已经尝试过代码块

1 个答案:

答案 0 :(得分:0)

好的,所以我终于明白了!

问题是新子类别表单上的路由错误。 我有:

<%= link_to "Subcategories Home", subcategories_index_path %>

我知道这是不正确的所以我把它改成了:

<%= link_to "Subcategories Home", subcategories_url %>

大多数情况下,我只是通过Rails Routing from the Outside In用精细的齿梳进行检查,直到我找到了正确的方法来回到子类别#index。