Rails - 每个块内的动态路由

时间:2017-02-12 20:05:13

标签: ruby-on-rails

这是我的第一个rails项目,我是所有代码的完全新手,而不仅仅是Ruby,所以如果我不能简明扼要地解释我的问题,请原谅我。

我有一个型号,Car,属于另一个型号,User。我希望我的汽车索引页面显示数据库中的所有汽车,因此我制作了自定义路线' / cars /',而不是由Rails生成的:user_id / cars /:id路由。

汽车信息正在输出到我的索引页面,但我无法确定如何生成返回车展页面的链接。

我确定它很简单,但我一整天都在阅读Rails指南和其他问题,但尚未修复它。

这是块:

                <% @cars.each do |car| %>                   
                <li>
                    <div class="well row <%= cycle('', 'white-bg')  %>">
                        <div class="col-sm-4 text-center">
                            <!-- thumbnail for car here -->
                        </div>
                        <div class="pull-left">
                            <%= link_to car.id, user_car_path(@user, car) do %>
                                <h3><%= car.year %> <%=car.make %> <%= car.model %></h3>
                            <% end %>
                        </div>
                        <div>
                            <h3 class="pull-right"><%= car.price %></h3>
                        </div>
                    </div>
                </li>                   
            <% end %>

路线:

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 =&gt;&#34; show&#34;,:controller =&gt;&#34; cars&#34;,:id =&gt;&#34; 1&#34;,:user_id =&gt ; 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) 型材#新

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)个人资料#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

获取/cars/:id(.:format)汽车#show

PATCH /cars/:id(.:format)cars#update

PUT /cars/:id(.:format)cars#update

DELETE /cars/:id(.:format)cars#destroy

1 个答案:

答案 0 :(得分:0)

我会建议一种替代解决方案,让您“免除”汽车资源 - 并且只为属于特定用户的汽车提供索引路线:

# 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

根据上下文,如果您能够为其他用户创建汽车,则可以将更多收集路线(新建,创建)移至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])