修改 URL 模式 Ruby Rails
Modify URL Pattern Ruby Rails
我正在尝试显示一个页面以显示当前用户的所有产品。因此,我在产品视图下创建了一个新页面 showall.html.erb。
我做了以下事情:
产品控制器
def showall
@products = current_user.products
end
路线
resources :products do
get :showall
end
我知道由于嵌套资源,URL 模式变成了
/products/:product_id/showall(.:format)
我如何真正摆脱 product_id 部分以实现 /products/showall 有一个特殊页面来呈现当前用户的所有产品。
您应该按如下方式更改路由定义:
resources :products do
collection do
get :showall
end
end
勾选对应的documentation.
希望对您有所帮助!
我正在尝试显示一个页面以显示当前用户的所有产品。因此,我在产品视图下创建了一个新页面 showall.html.erb。
我做了以下事情:
产品控制器
def showall
@products = current_user.products
end
路线
resources :products do
get :showall
end
我知道由于嵌套资源,URL 模式变成了
/products/:product_id/showall(.:format)
我如何真正摆脱 product_id 部分以实现 /products/showall 有一个特殊页面来呈现当前用户的所有产品。
您应该按如下方式更改路由定义:
resources :products do
collection do
get :showall
end
end
勾选对应的documentation.
希望对您有所帮助!