Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在rails简单表单中嵌入/嵌套多个表单的最佳方式是什么?_Ruby On Rails_Ruby On Rails 4_Simple Form_Nested Forms - Fatal编程技术网

Ruby on rails 在rails简单表单中嵌入/嵌套多个表单的最佳方式是什么?

Ruby on rails 在rails简单表单中嵌入/嵌套多个表单的最佳方式是什么?,ruby-on-rails,ruby-on-rails-4,simple-form,nested-forms,Ruby On Rails,Ruby On Rails 4,Simple Form,Nested Forms,我有一套模型,可以实现以下功能: 1) 画廊有许多图像,属于一本书 belongs_to :books has_many :images accepts_nested_attributes_for :books accepts_nested_attributes_for :images, :allow_destroy => true //画廊管理员 class GalleriesController < ApplicationController before_action

我有一套模型,可以实现以下功能:

1) 画廊有许多图像,属于一本书

belongs_to :books
has_many :images
accepts_nested_attributes_for :books
accepts_nested_attributes_for :images, :allow_destroy => true
//画廊管理员

 class GalleriesController < ApplicationController
  before_action :set_gallery, only: [:show, :edit, :update, :destroy]

  def index
    @galleries = Gallery.all
  end

  def show
  end

  def new
    @gallery = Gallery.new
  end

  def edit
  end
  def create
    @gallery = Gallery.new(gallery_params)
  end

  def update
  end

  def destroy
    @gallery.destroy
  end

  private
    def set_gallery
      @gallery = Gallery.find(params[:id])
    end
    def gallery_params
      params.require(:gallery).permit(:name, :book_id)
    end
end
//图像控制器

 class GalleriesController < ApplicationController
  before_action :set_gallery, only: [:show, :edit, :update, :destroy]

  def index
    @galleries = Gallery.all
  end

  def show
  end

  def new
    @gallery = Gallery.new
  end

  def edit
  end
  def create
    @gallery = Gallery.new(gallery_params)
  end

  def update
  end

  def destroy
    @gallery.destroy
  end

  private
    def set_gallery
      @gallery = Gallery.find(params[:id])
    end
    def gallery_params
      params.require(:gallery).permit(:name, :book_id)
    end
end
3) 一本书只有一个画廊

belongs_to :author
has_one :gallery

accepts_nested_attributes_for :author
accepts_nested_attributes_for :gallery
档案:

gem 'simple_form'
gem 'nested_form'
在图库的表单中,我得到了简单的表单字段,我在其中生成了表单字段,以便上传图像,因为创建图库需要多个表单字段,如下所示:

<%= simple_form_for(@gallery, html: { multipart: true }) do |f| %>
  <%= f.error_notification %>

  <div class="inputs">
    <%= f.input :name %>
    <%= f.input :book_id %>
    <%= f.simple_fields_for :image do |a| %>
        <%#= f.input :picture %>
        <%#= a.file_field :file %>
        <%= f.input_field :file, as: :file, multiple: true, name: 'gallery[image]' %>
    <% end %>
  </div>

  <div class="actions">
    <%= f.button :submit %>
  </div>
<% end %>

然后在我的图书表单中,我创建了一本书,我得到了一个表单,我想在其中嵌套图库表单,这样我就可以为每一本书创建添加一个图库。出于某种原因,它似乎不起作用,我不断地出错:

undefined method `simple_nested_form_for' for #<SimpleForm::FormBuilder:0x00000104d52640
未定义的方法'simple_nested_form_for'for#true})do | f |%>
这些错误似乎是由于它没有找到那个方法,我已经重新启动了服务器两次,但仍然得到相同的错误


我遇到的问题是将图库放在书中,这样我就可以创建书。

您正在尝试将表单包含在表单中。i、 e您在
{:multipart=>true})do | f |%>
下有

一次只能有一个表单实例。其他应该类似于

因此,请从您的图库表单中删除
。如果您需要任何其他帮助,可以通过ryan railscasts获得嵌套表单

对于嵌套模式下的简单表单,请遵循

<%= simple_form_for(@book, :html => { :multipart => true }) do |f| %>    
<%= f.simple_nested_form_for (@gallery) do |t| %>
    <%= t.simple_fields_for :gallery do |t| %>
    <%= render 'galleries/form' %>
    <% end %>
    <% end %>
<% end %>