[英]Rails create/update more than one model at the same time
我一直在努力使用嵌套表单,但我不知道为什么它不起作用。
表单更新/创建场地时不会投诉,但不会更新/关联场地信息。
问题:我想同时创建/更新一个模型及其关联模型之一。 请注意,控制器在“ admin”下命名空间
_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'
场地_控制器
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
场地图片_控制器
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
路线
namespace :admin do
resources :venues do
resources :venue_images
end
resources :users
end
在此先感谢您的帮助! 如果您需要更多代码,请告诉我。
好像您与Venue
具有has_many
关系(即has_many :venue_images
),然后此行
= f.fields_for(:venue_image, html: { class: 'form-horizontal', multipart: true }) do |vi|
应该
= f.fields_for(:venue_images, html: { class: 'form-horizontal', multipart: true }) do |vi|
而且您的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
注意复数venue_images
,并且我在venue_params
添加了:id
以使更新正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.