Rails同时创建/更新多个模型

时间:2015-07-15 19:30:40

标签: ruby-on-rails forms ruby-on-rails-4

我已经使用嵌套表格了一段时间,我不知道它为什么不起作用。

表单更新/创建没有投诉的场地,但没有相关的venue_image。

问题:我想同时创建/更新模型及其相关模型之一。请注意,控制器在“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'

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

路由

namespace :admin do
  resources :venues do
    resources :venue_images
  end
  resources :users
end

提前感谢您的帮助!如果您需要更多代码,请与我们联系。

1 个答案:

答案 0 :(得分:0)

好像你与Venuehas_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

请注意复数:id,并且还为venue_params添加了{{1}} 更新 才能正常工作。

相关问题