Rails – 每个块内的动态路由
这是我的第一个rails项目,我是所有代码的完全新手,而不仅仅是Ruby,所以如果我没有简明扼要地解释我的问题,请原谅我。
我有一个型号,Car,属于另一个型号,User。 我希望我的汽车索引页面显示数据库中的所有汽车,所以我制作了一个自定义路线’/ cars /’,而不是由Rails生成的:user_id / cars /:id路由。
汽车信息正在输出到我的索引页面,但我无法弄清楚如何生成链接回车展页面。
我确信这很简单,但我一整天都在阅读Rails指南和其他问题,但尚未解决。
这是块:
<div class="well row ">
路线:
get 'cars' => 'cars#index', as: :cars resources :users do resource :profile resources :cars, except: :index end
结束
控制器:
def new @user = User.find( params[:user_id] ) @car = @user.cars.build end # POST request to /users/:user_id/cars def create @user = User.find( params[:user_id] ) @car = @user.cars.build( car_params ) if @car.save redirect_to user_path( params[:user_id] ) end end # GET request to /users/:user_id/cars/:id def show @user = User.find( params[:user_id] ) @car = @user.cars.find( params[:id] ) end # GET request to /cars/ def index @cars = Car.all end
错误是:没有路由匹配{:action =>“show”,:controller =>“cars”,:id =>“1”,:user_id => nil}缺少必需的键:[:user_id]
我猜我在控制器中遗漏了一些东西,但我在那里尝试过的所有东西都会产生其他错误。
谢谢!
cars_path GET /cars(.:format)cars #index
POST /cars(.:format)cars #create
new_car_path GET /cars/new(.:format)cars#new
edit_car_path GET /cars/:id/edit(.:format)cars#edit
car_path GET /cars/:id(.:format)cars#show
PATCH /cars/:id(.:format)cars#update
PUT /cars/:id(.:format)cars#update
DELETE /cars/:id(.:format)cars#destroy
new_user_profile_path GET /users/:user_id/profile/new(.:format)个人资料#new
edit_user_profile_path GET /users/:user_id/profile/edit(.:format)
型材#编辑
user_profile_path GET /users/:user_id/profile(.:format)
型材#秀
PATCH /users/:user_id/profile(.:format)profiles#update
PUT /users/:user_id/profile(.:format)个人资料#update
DELETE /users/:user_id/profile(.:format)profiles#destroy
POST /users/:user_id/profile(.:format)profiles #create
user_cars_path GET /users/:user_id/cars(.:format)cars #index
POST /users/:user_id/cars(.:format)cars #create
new_user_car_path GET /users/:user_id/cars/new(.:format)cars#new
GET /cars/:id/edit(.:format)cars#edit
GET /cars/:id(.:format)汽车#show
PATCH /cars/:id(.:format)cars#update
PUT /cars/:id(.:format)cars#update
DELETE /cars/:id(.:format)cars#destroy
我会建议一种替代解决方案,你可以“免除”汽车资源 – 并且只为属于特定用户的汽车提供索引路线:
# routes.rb resources :cars resources :users do resources :cars, module: 'users', only: [:index] end
# app/controller/cars_controller.rb class CarsController < ApplicationController # GET /cars def index @cars = Car.all end # show, create, delete, new... end # app/controller/users/cars_controller.rb class Users::CarsController < ApplicationController # GET /users/:user_id/cars def index @user = User.includes(:cars).find(params[:user_id]) @cars = @user.cars end end
根据上下文,如果您能够为其他用户创建汽车,则可以将更多的收集路径(new,create)移动到Users::CarsController
。 嵌套成员路线(作用于单个记录)很少是必要的。 您可以使用shallow: true
选项来避免它:
resources :users do resources :cars, shallow: true end
这使您可以通过以下方式路由到汽车:
link_to(@car.name, car_path(@car)) # or link_to(@car.name, @car)
如果您决定保留当前设置,则使用一个或多个关键字路由到嵌套资源:
link_to(@car.name, user_car_path(user: @user, id: @car)) # or link_to(@car.name, [@user, @car])