Ruby 2.7.0 on Rails 6.1, 2个独立的post形成一个视图



希望你有一个美好的一天,喝点咖啡,回复一些表格。问题:正如我的标题所述,我试图在一个视图上创建两个表单。我是ruby on rails新手。

我的控制器函数:控制器名称为border_rotation:

def create
if params[:export_submit]
@border_rotation_export = BorderRotationExport.new(border_rotation_export_params)
respond_to do |format|
if @border_rotation_export.save
flash[:success] = "Export successfully created"
format.html { render :new }
else
flash[:error] = "Export was not created."
end
end
else
@border_rotation_import = BorderRotationImport.new(border_rotation_import_params)
respond_to do |format|
if @border_rotation_import.save
flash[:success] = "Export successfully created"
format.html { render :new }
else
flash[:error] = "Export was not created."
end
end
end
end 
def new
@border_rotation_export = BorderRotationExport.new
@border_rotation_import = BorderRotationImport.new
end
private 
def border_rotation_export_params
params.require(:border_rotation_export).permit(:exporter_name,:vehicle_color,:rot_num,:current_date,:current_time,:goods_description,:license_num,:entry)
end
def border_rotation_import_params
params.require(:border_rotation_import).permit(:importer_name,:vehicle_color,:rot_num,:current_date,:current_time,:goods_description,:license_num,:entry)
end 

我新建的View表单:它有2个表单,包含在bootstrap标签

中。
<%=  form_for @border_rotation_export, url: rotation_create_path, method: :post do |f|%>
<lable>Importer Name: </lable><%= f.text_field :importer_name, class: "form-control", placeholder: "Importer Name"%>
<lable>Vehicle Color: </lable><%= f.text_field :vehicle_color, class: "form-control", placeholder: "Vehicle Color"%>
**its fields**
<% end %> 

<%=  form_for @border_rotation_import, url: rotation_create_path, method: :post do |f|%>
<lable>Exporter Name: </lable><%= f.text_field :exporter_name, class: "form-control", placeholder: "Exporter Name"%>
<lable>Vehicle Color: </lable><%= f.text_field :vehicle_color, class: "form-control", placeholder: "Vehicle Color"%>
**its fields**
<% end %>

my new.html.rbform中的第一个参数不能包含nil或为空

以红色高亮显示

<%=  form_for @border_rotation_export, url: rotation_create_path, method: :post do |f|%>

我的猜测是,它提交两个表单,但只有一个表单的参数与输入数据。提交后,它保存到数据库中但错误提示

* *路线* *

get 'rotation/create', to: 'border_rotation#create'

post 'rotation/create', to: 'border_rotation#create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"Cu52CIDgrY0b7Yk6edkd7+RTl5yR4qSEqPPrqWtM0nIQVDvw7eYDF36zduJPLjI+vVNqCfgtLcMDUEkW6qDOdQ==",
"border_rotation_import"=>
{"importer_name"=>"john",
"vehicle_color"=>"red",
"rot_num"=>"11sssfeeea",
"current_date"=>"2021-09-22",
"current_time"=>"09:37",
"goods_description"=>"yogurt",
"license_num"=>"c-11223",
"entry"=>"c1223"},
"import_submit"=>"Submit"}

提前感谢

你可以用更少的冗余设置控制器:

# config/routes.rb
resources :rotations, only: [:new, :create]
class BorderRotationsController < ApplicationController
# GET /rotations/new
def new
populate_forms
end
# POST /rotations
def create
resource = model_class.new(create_params)
set_ivar(resource) # sets @border_rotation_export or @border_rotation_import
if resource.save  
flash[:success] = "#{humanized} successfully created"
redirect_to action: :new
else
populate_forms
flash[:error] = "#{humanized} could not be created - please try again."
render :new
end
end
private
# gets the model class via params[:subtype]
def model_class
@model_class ||= begin do
if params[:border_rotation_export].present?
BorderRotationExport
else
BorderRotationImport 
end
end
end
def humanized
model_class == BorderRotationExport ? 'Export' : 'Import'
end
def set_ivar(value)
instance_variable_set(
"@#{param_key}",
value
)  ​
​end
# just sets up the instance variables for the form
def populate_forms
@border_rotation_export ||= BorderRotationExport.new
@border_rotation_import ||= BorderRotationImport.new
end
# border_rotation_export or border_rotation_import
def param_key
model_class.model_name.param_key
end

def create_params
require(param_key).permit(
:exporter_name, :vehicle_color, :rot_num,
:current_date,:current_time, :goods_description,
:license_num, :entry
)
end

然后使用偏导,这样就可以重复使用相同的形式:

# app/views/border_rotations/new.html.erb
<%= render partial: 'form', 
locals: { border_rotation: @border_rotation_export } %>
<%= render partial: 'form', 
locals: { border_rotation: @border_rotation_import } %>
# app/views/border_rotations/new.html.erb
<%= form_with model: border_rotation, url: rotations_path do |f| %>
<div class="field">
<%= f.label :importer_name %>
<%= f.text_field :importer_name, class: "form-control" %>
</div>
<div class="field">
<%= f.label :importer_name %>
<%= f.text_field :importer_name, class: "form-control" %>
</div>
# ...
<% end %>

如果需求不同,使用两个单独的路由/控制器和继承,而不是在大量的新代码路径中涌现。

最新更新