[英]Rails create/update more than one model at the same time
I have been struggeling with a nested form for a while and I dont know why it is not working. 我一直在努力使用嵌套表单,但我不知道为什么它不起作用。
The form updates/creates the venue without complaint, but not the associated venue_image. 表单更新/创建场地时不会投诉,但不会更新/关联场地信息。
Problem: I want to create/update a model and one of its associated models at the same time. 问题:我想同时创建/更新一个模型及其关联模型之一。 Please note that the controllers are namespaced under "admin"
请注意,控制器在“ admin”下命名空间
_form.haml _form.haml
.col-md-12
= form_for([:admin, venue], html: { class: 'form-horizontal', multipart: true }) do |f|
- if venue.errors.any?
.col-md-12.alert.alert-danger{role: "alert"}
%h2
= pluralize(venue.errors.count, "Error")
%ul
- venue.errors.full_messages.each do |message|
%li= message
.form-group
= f.label :name, class: 'col-md-2 control-label'
.col-md-10
= f.text_field :name, class: 'form-control'
.form-group
= f.label :category, class: 'col-md-2 control-label'
.col-md-10
= f.text_field :category, class: 'form-control'
.form-group
= f.label :description, class: 'col-md-2 control-label'
.col-md-10
= f.text_area :description, rows: 5, class: 'form-control'
.form-group
= f.label :street, class: 'col-md-2 control-label'
.col-md-10
= f.text_field :street, class: 'form-control'
.form-group
= f.label :zip, class: 'col-md-2 control-label'
.col-md-10
= f.text_field :zip, class: 'form-control'
.form-group
= f.label :city, class: 'col-md-2 control-label'
.col-md-10
= f.text_field :city, class: 'form-control'
.form-group
= f.label :homepage, class: 'col-md-2 control-label'
.col-md-10
= f.url_field :homepage, class: 'form-control'
%h3 Add images
= f.fields_for(:venue_image, html: { class: 'form-horizontal', multipart: true }) do |vi|
.form-group
= vi.label :name, class: 'col-md-2 control-label'
.col-md-10
= vi.input :name, label: false, class: 'form-control'
.form-group
= vi.label :venue_id, class: 'col-md-2 control-label'
.col-md-10
= vi.input :venue_id, label: false, class: 'form-control'
.form-group
= vi.label :default, class: 'col-md-2 control-label'
.col-md-10
= vi.input :default, as: :radio_buttons, label: false, class: 'form-control radio radio-inline'
.form-group
= vi.label :image_file, class: 'col-md-2 control-label'
.col-md-10
= vi.file_field :image_file, label: false, class: 'form-control'
.actions
= f.submit 'Save', class: 'btn btn-primary pull-right'
= link_to 'Cancel', :back, class: 'btn btn-default'
venues_controller 场地_控制器
class Admin::VenuesController < Admin::BaseController
before_action :set_venue, only: [:show, :edit, :update, :destroy]
def index
@venues = Venue.all
end
def show
end
def new
@venue = Venue.new
@venue.venue_images.build
end
def edit
end
def create
@venue = Venue.new(venue_params)
if @venue.save
redirect_to admin_venue_path(@venue), notice: 'Venue was successfully created.'
else
render :new
end
end
def update
if @venue.update(venue_params)
redirect_to admin_venue_path(@venue), notice: 'Venue was successfully updated.'
else
render edit_admin_venue
end
end
def destroy
@venue.destroy
redirect_to admin_venues_url, notice: 'Venue was successfully destroyed.'
end
private
def set_venue
@venue = Venue.find(params[:id])
end
def venue_params
params.require(:venue).permit(:name, :category, :description, :street, :zip, :city, :homepage,
venue_image_attributes: [:name, :default, :image_file])
end
end
venue_images_controller 场地图片_控制器
class Admin::VenueImagesController < Admin::BaseController
def new
image = VenueImage.new
render locals: { image: image }
end
def create
# TODO: Remove @ if possible
@image = VenueImage.new(venue_images_params)
if @image.save
redirect_to admin_venue_path(@image.venue.id), notice: 'Image was successfully created.'
else
render admin_new_venue_path
end
end
private
def venue_images_params
params.require(:venue_image).permit(:name, :default, :image_file, :venue_id)
end
end
routes 路线
namespace :admin do
resources :venues do
resources :venue_images
end
resources :users
end
Thanks in advance for any help! 在此先感谢您的帮助! Please let me know if you need more of the code.
如果您需要更多代码,请告诉我。
Seems like you have a has_many
relation with the Venue
(ie, has_many :venue_images
), then this line 好像您与
Venue
具有has_many
关系(即has_many :venue_images
),然后此行
= f.fields_for(:venue_image, html: { class: 'form-horizontal', multipart: true }) do |vi|
should be 应该
= f.fields_for(:venue_images, html: { class: 'form-horizontal', multipart: true }) do |vi|
And your venue_params
method should be like below 而且您的
venue_params
方法应如下所示
def venue_params
params.require(:venue).permit(:id, :name, :category, :description, :street, :zip, :city, :homepage, venue_images_attributes: [:id, :name, :default, :image_file])
end
Notice the plural venue_images
and also I added :id
to venue_params
for Update to work correctly. 注意复数
venue_images
,并且我在venue_params
添加了:id
以使更新正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.