ruby-on-rails - 带有 simple_form : add new table row with link_to_add 的 Rails

标签 ruby-on-rails simple-form formtastic fields-for cocoon-gem

我在通过单击 link_to_add 函数链接添加新表行时遇到问题。

最佳答案

因为我花了一些搜索和尝试错误才找到这里是我的解决方案。它的关键是使用 cocoon gem由 nathanvda(而不是由 ryanb 提供的 nested_form),它允许在插入新字段集的位置上定义一个 jquery 选择器(称为 data-association-insertion-node)。在我的示例中,我使用了 HAML 和 Twitter Bootstrap 代码,但您可以轻松地将其替换为您自己的最爱。

/app/views/users/_form.html.haml :

= simple_form_for @user, :html => {:class => "form-horizontal"} do |f|
  = render "shared/error_messages", :target => @user
  .row-fluid
    .span12
      .fieldset
        = f.input :username, :label => "Username:", :placeholder => "Username"

        %table.table
          %thead
            %tr
              %th
                %i.icon-calendar
                Date
              %th
                %i.icon-pencil
                Description
              %th
                %i.icon-cog
                Action
          %tbody#user_events
            = f.simple_fields_for :user_events do |event|
              = render "user_event_fields", :f => event

        = link_to_add_association f, :user_events, :"data-association-insertion-node" => "tbody#user_events", :"data-association-insertion-method" => "append", :partial => "user_event_fields", :class => "btn" do
          %i.icon-plus
          Add new event

        %p.help-block
          %strong Please note:
          Events are only removed after confirming the changes with
          %span.label
            %i.icon-ok
            Update settings!

        = f.button :submit, :class => "btn" do
          %i.icon-ok
          Update settings!

/app/views/users/_user_event_fields.html.haml :
%tr.nested-fields
  %td= f.input_field :date, :as => :date_month_year, :class => "tooltip-bottom input-small", :rel => "tooltip", :title => "Date of event", :disabled => params[:action] == "show"
  %td= f.input_field :description, :placeholder => "Description", :class => "input-large tooltip-bottom", :rel => "tooltip", :title => "Description of event<br/>Use <strong>Markdown</strong> to format your text.", :disabled => params[:action] == "show"
  %td= f.input_field :label, :placeholder => "Label", :class => "input-medium tooltip-bottom", :rel => "tooltip", :title => "Label of event", :disabled => params[:action] == "show"
  %td= f.input_field :label_class, :collection => [["Green", "label-success"], ["Yellow", "label-warning"], ["Red", "label-important"], ["Blue", "label-info"], ["Black", "label-inverse"]], :include_blank => "Grey", :class => "input-small tooltip-bottom", :rel => "tooltip", :title => "Label color of event", :disabled => params[:action] == "show"
  %td
    = link_to_remove_association f, :class => "btn" do
      %i.icon-remove

请注意 .nested-fields标签上的类对于删除链接的工作至关重要。

关于ruby-on-rails - 带有 simple_form : add new table row with link_to_add 的 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10995932/

相关文章:

ruby-on-rails - 未初始化的常量 Authlogic::TestCase::ControllerAdapters (NameError)

css - 使用 simple_form 如何在这些内部标签和外部标签上获取此类?

ruby-on-rails - 简单形式的 Rails 3.2 嵌套路由

ruby-on-rails - 有_许多 :through formtastic multi-select field

ruby-on-rails - 为什么 accepts_nested_attributes_for 对我不起作用? ( rails 3)

javascript - Rails 3.1.1 中的 jquery 总是给出 DOMException

ruby-on-rails-3 - 简单表单上的只读复选框仍然可以更新

ruby-on-rails - rails : simple_form : Getting an empty string from checkbox collection

ruby-on-rails - Rails 3. 使用 Formtastic 有条件地显示字段

ruby-on-rails - 为 MultiJson :Module for jbuilder 获取未定义的方法 `dump'