Ruby on rails Rails 4:form_用于嵌套模型
我有三种型号:客户、汽车和ParkingRate。客户有很多车,而且车有很多停车费。我在客户端页面上有一个表单,它创建了一个与该客户端关联的汽车。我不知道如何在表单中添加一个停车费率字段,这样当为该客户创建一辆车时,也会为该车创建一个停车费率 我的代码如下所示: client.rbRuby on rails Rails 4:form_用于嵌套模型,ruby-on-rails,nested-form-for,Ruby On Rails,Nested Form For,我有三种型号:客户、汽车和ParkingRate。客户有很多车,而且车有很多停车费。我在客户端页面上有一个表单,它创建了一个与该客户端关联的汽车。我不知道如何在表单中添加一个停车费率字段,这样当为该客户创建一辆车时,也会为该车创建一个停车费率 我的代码如下所示: client.rb class Client < ActiveRecord::Base has_many :cars, dependent: destroy end class客户端
class Client < ActiveRecord::Base
has_many :cars, dependent: destroy
end
class客户端
car.rb
class Car < ActiveRecord::Base
belongs_to :client
has_many :parking_rates
end
class-Car
停车费
class ParkingRate < ActiveRecord::Base
belongs_to :car
end
class ParkingRate
在客户端页面(client/:id),我有一个表单来创建与该客户端关联的汽车,如下所示:
视图/客户端/show.html.erb:
<h1>Client information</h1>
... client info ...
<%= render 'cars/form' %>
<%= 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 %>
客户端信息
... 客户信息。。。
视图/cars/_form.html.erb:
<h1>Client information</h1>
... client info ...
<%= render 'cars/form' %>
<%= 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_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
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
class客户端控制器
cars_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
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
class CarsController
有了这个,我可以为给定的客户添加汽车,但我也希望在相同的表单上为汽车添加停车费。所以当我使用这个表单创建一辆车时,我想创建一个相关的停车率。助手的
表单使用[@client,@client.comments.build]
作为模型对象,因此我不确定如何在同一表单中引用停车费率
模型。我认为解决方案是为
助手使用一个fields\u,它的模型参考是什么,我需要向汽车和客户控制器添加什么?在client.rb中,添加行
accepts_nested_attributes_for :cars
已尝试在汽车模型中使用accepts\u nested\u attributes\u:parking\u rates,dependent:destroy
。阅读上的Rails指南可以帮上大忙。