【问题标题】:Rails 4: form_for with nested modelRails 4:带有嵌套模型的 form_for
【发布时间】:2014-03-20 18:07:28
【问题描述】:

我有三个模型:Client、Car 和 ParkingRate。客户有很多汽车,汽车有很多停车费率。我在客户端页面上有一个表单,用于创建与该客户端关联的汽车。我不知道该怎么做是在该表单中添加一个 park_rate 字段,以便在为该客户创建汽车时,也为该汽车创建停车费率。

我的代码如下:

client.rb

class Client < ActiveRecord::Base
  has_many :cars, dependent: destroy
end

汽车.rb

class Car < ActiveRecord::Base
  belongs_to :client
  has_many :parking_rates
end

停车费率.rb

class ParkingRate < ActiveRecord::Base
  belongs_to :car
end

在客户端页面 (client/:id) 上,我有一个表单来创建与该客户端关联的汽车,如下所示:

views/clients/show.html.erb:

<h1>Client information</h1>
... client info ...
<%= render 'cars/form' %>

views/cars/_form.html.erb:

<%= form_for([@client, @client.cars.build]) do |f| %>
  <p>
    <%= f.label :vehicle_id_number %><br>
    <%= f.text_field :vehicle_id_number %>
  </p>
  <p>
    <%= f.label :enter_date %><br>
    <%= f.text_field :enter_date %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

Clients 和 Cars 控制器如下所示:

clients_controller.rb:

class ClientsController < ApplicationController

def new
  @client = Client.new
end

def create
  @client = Client.new(client_params)
  if @client.save
    redirect_to @client
  else
    render 'new'
  end
end

def show
  @client = Client.find(params[:id])
end

def index
  @clients = Client.all
end

def edit
  @client = Client.find(params[:id])
end

def update
  @client = Client.find(params[:id])

  if @client.update(client_params)
    redirect_to @client
  else
    render 'edit'
  end
end

def destroy
  @client = Client.find(params[:id])
  @client.destroy

  redirect_to clients_path
end

private
  def client_params
    params.require(:client).permit(:first_name, :last_name)
  end
end

cars_controller.rb:

class CarsController < ApplicationController

def create
  @client = Client.find(params[:client_id])
  @car = @client.cars.create(car_params)
  @parking_rate = @car.parking_rates.create(rate_params)
  redirect_to client_path(@client)
end

def show
  @client = Client.find(params[:client_id])
  @car = Car.find(params[:id])
end

def edit
  @client = Client.find(params[:client_id])
  @car = Car.find(params[:id])
end

def update
  @client = Client.find(params[:client_id])
  @car = Car.find(params[:id])
  @car.update(car_params)

  redirect_to client_path(@client)
end

def destroy
  @client = Client.find(params[:client_id])
  @car = @client.cars.find(params[:id])
  @car.destroy
  redirect_to client_path(@client)
end

private
  def car_params
    params.require(:car).permit(:vehicle_id_number, :enter_date, :rate)
  end

  def rate_params
    params.require(:parking_rate).permit(:rate)
  end
end

有了这个,我可以将汽车添加到给定的客户,但我还想在同一个表单上为汽车添加停车费率。因此,当我使用此表单创建汽车时,我想创建一个相关的停车费。 form_for 助手使用[@client, @client.comments.build] 作为模型对象,所以我不确定如何以相同的形式引用parking_rate 模型。我认为解决方案是使用 fields_for 助手,模型参考是什么,我需要向汽车和客户端控制器添加什么?

【问题讨论】:

  • 已尝试在汽车模型中使用accepts_nested_attributes_for :parking_rates, dependent: destroy
  • 阅读 Forms 上的 Rails 指南可以帮助你很多。

标签: ruby-on-rails nested-form-for


【解决方案1】:

在client.rb中,添加一行

accepts_nested_attributes_for :cars

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-27
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多