不能让Cant可以与我的类别和子类别一起使用。我也使用了friendly_id gem。
这是我的类别控制器
class CategoriesController < ApplicationController
def index
@categories = Category.all
@oval = @categories[0]
@atv = @categories[1]
@boat = @categories[2]
@fab = @categories[3]
@chassis = @categories[4]
@classic = @categories[5]
@dirt = @categories[6]
@dragracing = @categories[7]
@audio = @categories[8]
@engine = @categories[9]
@tune = @categories[10]
@enginecom = @categories[11]
@exhaust = @categories[12]
@golfcart = @categories[13]
@hotrod = @categories[14]
@karts = @categories[15]
@modern = @categories[16]
@motorcycle = @categories[17]
@muscle = @categories[18]
@offroad = @categories[19]
@racingitem = @categories[20]
@accessories = @categories[21]
@position = @categories[22]
@racingeq = @categories[23]
@roadracing = @categories[24]
@safety = @categories[25]
@snowmobile = @categories[26]
@tw = @categories[27]
@tools = @categories[28]
@rvs = @categories[29]
@tractor = @categories[30]
@trailer = @categories[31]
@transmission = @categories[32]
@truckpart = @categories[33]
@drift = @categories[34]
@import = @categories[35]
end
例如,我现在的网址是 本地主机:3000 / categories / 4 / subcategories / 27
我希望它成为
localhost:3000 / categories / dragracing / subcategories / pro-mod
这是我的路线
Rails.application.routes.draw do
resources :channels
resources :discussions do
resources :replies
end
resources :comments
resources :blogs
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
mount ActionCable.server => '/cable'
get 'users/show'
get 'about', to: 'pages#about'
get 'directory', to: 'pages#directory'
get 'contact', to: 'pages#contact'
get 'privacy', to: 'pages#privacy'
get 'faq', to: 'pages#faq'
get '/myads', to: 'posts#myads'
get 'partner', to: 'pages#partner'
get '/sitemap' => 'sitemap#index', defaults: {format: "xml"}
root 'categories#index'
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout', sign_up: 'register' }, controllers: { registrations: "registrations"}
resources :users
resources :mes
resources :pricing, only:[:index]
resources :posts
resource :subscription
resources :categories do
resources :subcategories
end
end
答案 0 :(得分:0)
您要使用的路线的描述为:
# /categories/:category_id/subcategories/:id
这意味着您发送的任何内容(而非:category_id
都将以params[:category_id]
的形式在您的控制器中显示,与:id
=> params[:id]
相同)。
因此,如果您发送/categories/dragracing/subcategories/pro-mod
,则参数将为:
{ 'category_id' => 'dragracing', 'id' => 'pro-mod' }
按照您当前的代码状态,此端点将路由到subcategories#show
。
# category_subcategory GET /categories/:category_id/subcategories/:id(.:format) subcategories#show
因此,您需要一个控制器。您无需在URL中发送id
或数字,只需正确处理即可:
class SubcategoriesController < ApplicationController
def show
@category = Category.friendly.find params[:category_id] # dragracing
@subcategory = Subcategory.friendly.find params[:id] # pro-mod
end
end