没有路线匹配[GET]" / users / 1 / categories / 5"

时间:2015-11-12 21:22:49

标签: ruby-on-rails routes

我尝试查询用户所喜欢的所有的数据来自特定的类别。 EG: / users / 1 / categories / 5

我有一点Rails-101问题,无法找出服务器告诉我路由不存在的原因。

参数是正确的。该路由存在于Routes.rb中。我唯一能想到的是路线的出现顺序。或者也许是由于路由的SPA特性所致。

routes.rb中:

Rails.application.routes.draw do


  ACCEPTS_JSON = lambda {|request|
    request.accepts.include?(:json)
  }

  get "/users/email" => "users#id"
  get "/items/:id/like" => "items#checkhasliked"
  get "/users/:u_id/categories/:c_id " => "users#categoryitems" // should do the trick?

  scope constraints: ACCEPTS_JSON do
    resources :users
    resources :categories
    resources :items
  end

  get "/*path" => "app#main"

  post "/users" => "users#create"
  post "/items" => "items#create"

  post "/items/:id/like" => "items#like"

  post "/items/:id/unlike" => "items#unlike"

  post "/sessions" => "sessions#create"

  root 'app#main'
end

UsersController:

    class UsersController < ApplicationController

        ...

        def categoryitems
            render json: {head: :ok}
        end
    end

ajax电话:

fetchCategoryItems: function(cID){
    var uID = this.props.params.id   // user ID
    var cID = cID   // category ID 

    $.ajax({
        type: "GET",
        url: "/users/" + uID + "/categories/" + cID,
        dataType: 'json',
        success: function(data){
            this.setState({didFetchData: 'true', items: data.items})
        }.bind(this),
        error: function(data){
            alert("error! couldn't fetch user's items-by-category")
        }
    })
}

1 个答案:

答案 0 :(得分:1)

我会在你的路线文件中使用它

  resources :users, only: [:show] do
     resources :categories, only: [:show], to:  'users#categoryitems'
  end