【问题标题】:Cocoon gem won't render partial using simple_form and Zurb4 under Rails 4Cocoon gem 不会在 Rails 4 下使用 simple_form 和 Zurb4 渲染部分
【发布时间】:2023-03-23 10:36:01
【问题描述】:

* 编辑 *

我正在使用 Foundation 主题,我认为问题出在其中。我创建了一个新的基本应用程序,在添加主题之前没有任何问题。当我找到罪魁祸首时,我会更新这个。

* 以前的编辑 *

我想消除场景中的一些变量,所以我使用 form_for 而不是 simple_form_for 对此进行了测试,但没有成功。

在 Cocoon 的文档中,它提到了较新版本的 Bootstrap 的 :wrapper => 'bootstrap' 选项,我确实使用了 simple_form 提供的类似基础包装器,但没有成功。

= simple_form_for @group, :wrapper => 'foundation', :html => {:class => 'form-vertical' } do |f|
...
= f.simple_fields_for :solicitations do |solicitation|
  = render 'solicitation_fields', :f => solicitation
.links
  = link_to_add_association "Add Invitation?", f, :solicitations, :render_options => { :wrapper => 'foundation' }

如果它是一个因素,我删除了大部分样式。不是。

FWIW,我正在使用 Rails 网站上推荐的 Ruby 2.1。另外,我因过度撞击墙壁而头疼。

底线问题:为什么显示部分的触发器不起作用?

* 结束编辑 *

我非常想使用 Nathanvda 的 Cocoon gem,但在单击 link_to_add_to_association 后无法显示关联的嵌套表单。确实会出现确认消息。我似乎遗漏了一些东西,或者我对 simple_form、zurb 和 cocoon 的使用使解决方案复杂化。

  • 导轨 4.0.2
  • simple_form 3.0.1
  • zurb-foundation 4.3.2
  • 茧1.2.5

文件:

... Models

class Group < ActiveRecord::Base
  has_many :solicitations, :dependent => :destroy
  accepts_nested_attributes_for :solicitations, reject_if: :all_blank

class Solicitation < ActiveRecord::Base
  belongs_to :group

... Controller

class GroupsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_group, only: [:show, :edit, :update, :destroy]
  before_action :set_current_user, only: [:index, :edit, :update, :create]

  def new
    @group = Group.new
    respond_to do |format|
      format.html
      format.json { render json: @group }
    end
  end

  def create
    @group = Group.new(group_params)
    respond_to do |format|
      if @group.save
        format.html { redirect_to action: :index, notice: 'Group was successfully created.' }
        format.json { render json: @group, status: :created, location: @group }
      else
        format.html { render action: "new" }
        format.json { render json: @group.errors, status: :unprocessable_entity }
      end
    end
  end

  private

    def group_params
      params.require(:group).permit( :name, :id,
        solicitations_attributes: [ :id, :email, :name, :role, :_destroy ] )

... Form Partial _solicitation_fields.html.haml

= simple_form_for @group, :html => {:class => 'form-vertical' } do |f|
  = f.error_notification
  .fields
    .row
      .large-12.columns
        = f.input :name, :required => true, placeholder: 'A descriptive group name'
    %hr
      .large-12.columns
      %h4 Group Solicitations
      .large-12.columns
        %table.responsive
          %thead
            %tr
              - headings = ["* Name", "* Email", "* Role", ""]
              - headings.each do |heading|
                %th= heading
          %tbody
            = f.simple_fields_for :solicitations do |solicitation|
              = render 'solicitation_fields', :f => solicitation
            .links
              = link_to_add_association "Add Solicitation?", f, :solicitations, confirm: "Does this work?"
      .large-12.columns
      .row
        .large-12.columns
          = f.error :base
          = f.button :submit, class: "button radius"

... groups.js
$(document).ready(function() {
    $('#solicitations').bind('cocoon:before-insert', function(e,solicitation_to_be_added) {
        solicitation_to_be_added.fadeIn('slow');
    });

    $('#solicitations').bind('cocoon:after-insert', function(e, added_solicitation) {
        //added_solicitation.css("background","red");
    });

    $('#solicitations').bind('cocoon:before-remove', function(e, solicitation) {
        $(this).data('remove-timeout', 1000);
        solicitation.fadeOut('slow');
    })

});...

坦率地说,我在 js 方面很弱。我已经为此工作了几天并且不想退出,所以任何帮助或指导将不胜感激。我已经看到 Bootstrap 需要一些内联包装器才能使用 cocoon 和 simple_form,但似乎 simple_form 初始化程序与 github 上 nathanvda 的演示示例相比发生了很大变化。

此外,当我明确地将 @group.solicitations.build 放入控制器操作中时,我可以显示部分内容,但我在演示中看不到该过程。

【问题讨论】:

    标签: ruby-on-rails zurb-foundation simple-form nested-forms cocoon-gem


    【解决方案1】:

    我决定用 nested_form gem 代替 cocoon gem,但它也失败了。问题在于主题及其将 gem 的 js 指令放在其自己的特殊清单中的要求。主题的文档没有提到这一点。我没有想到 cocoon.js 没有加载。现在工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      相关资源
      最近更新 更多