Skip to content

Instantly share code, notes, and snippets.

@melcher
Forked from kritik/gist:1256399
Created December 28, 2013 20:33

Revisions

  1. melcher renamed this gist Dec 28, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Vladimir Krylov revised this gist Oct 1, 2011. 1 changed file with 33 additions and 28 deletions.
    61 changes: 33 additions & 28 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -2,26 +2,10 @@ class Product < ActiveRecord::Base

    has_many :material_product_mappings, :dependent => :destroy, :inverse_of => :product
    has_many :materials, :through => :material_product_mappings, :autosave => true

    accepts_nested_attributes_for :material_product_mappings # really needed feature
    #...

    rails_admin do
    list do
    field :material_product_mappings do
    label "Materials"
    visible true
    end
    #...
    end
    edit do
    form_builder :nested_form_for
    field :material_product_mappings do
    label "Materials"
    partial "edit_material_product_mappings"
    end
    #...
    end
    end


    end

    @@ -35,20 +19,28 @@ class MaterialProductMapping < ActiveRecord::Base
    def name
    "#{self.material.name} (#{self.material.unit.name}) x #{self.num_units}"
    end

    rails_admin do
    label "Materials"
    visible false
    end
    end

    # file .../config/initializers/rails_admin.rb
    RailsAdmin.config do |config|
    config.model MaterialProductMapping do
    edit do
    form_builder :nested_form_for
    field :material_product_mappings do
    label "Materials"
    partial "edit_material_product_mappings"
    end
    #...
    end
    end
    end

    # file .../app/views/rails_admin/main/_edit_material_product_mappings.html.erb

    <div class="field <%= field.dom_id %>">
    <% _saved_object = @object %>
    <% _saved_form = form %>
    <% _saved_field = field %>
    <%# add here basic element's fileds if you have them %>
    <%= label_tag(field.dom_id, field.label) %>
    <div style="clear:left;padding-left:100px">
    <%= form.fields_for(:material_product_mappings) %>
    @@ -74,8 +66,21 @@ end
    <%= ff.collection_select('material_id',Material.active,:id,:name,:prompt => false) %>
    <%= ff.link_to_remove('Remove') %>

    # also need to copy app/views/layouts/rails_admin/_head.html.haml from the rails_admin installation to
    # your app and modify it to include nested_form's JS. add the following line:

    - head_javascript "nested_form"
    I also had to override javascripts to make nested resources working. I hope that this will be fixed in future releases
    # file .../app/assets/javascripts/rails_admin/rails_admin.js
    //= require rails_admin/jquery-1.6.2.js
    //= require rails_admin/jquery_ujs.js
    //= require rails_admin/jquery-ui-1.8.14.js
    //= require rails_admin/jquery.ui.timepicker.js
    //= require rails_admin/ra.datetimepicker.js
    //= require rails_admin/ra.filter-box.js
    //= require rails_admin/activo/application.js
    //= require rails_admin/activo/jquery.tipsy.js
    //= require rails_admin/ra.filtering-multiselect.js
    //= require rails_admin/ra.filtering-select.js
    //= require rails_admin/ra.remote-form.js
    //= require rails_admin/ra.timeline.js
    //= require rails_admin/jquery.colorpicker.js
    //= require jquery_nested_form
    //= require rails_admin/application.js

  3. @crosebrugh crosebrugh revised this gist Aug 6, 2011. No changes.
  4. @crosebrugh crosebrugh revised this gist Aug 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ class MaterialProductMapping < ActiveRecord::Base
    "#{self.material.name} (#{self.material.unit.name}) x #{self.num_units}"
    end

    ra_config do
    rails_admin do
    label "Materials"
    visible false
    end
  5. @crosebrugh crosebrugh created this gist Aug 5, 2011.
    81 changes: 81 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    class Product < ActiveRecord::Base

    has_many :material_product_mappings, :dependent => :destroy, :inverse_of => :product
    has_many :materials, :through => :material_product_mappings, :autosave => true

    #...

    rails_admin do
    list do
    field :material_product_mappings do
    label "Materials"
    visible true
    end
    #...
    end
    edit do
    form_builder :nested_form_for
    field :material_product_mappings do
    label "Materials"
    partial "edit_material_product_mappings"
    end
    #...
    end
    end

    end

    class MaterialProductMapping < ActiveRecord::Base
    belongs_to :product, :inverse_of => :material_product_mappings
    belongs_to :material, :inverse_of => :material_product_mappings

    validates :num_units,
    :numericality => { :greater_than_or_equal_to => 0 }

    def name
    "#{self.material.name} (#{self.material.unit.name}) x #{self.num_units}"
    end

    ra_config do
    label "Materials"
    visible false
    end
    end


    # file .../app/views/rails_admin/main/_edit_material_product_mappings.html.erb

    <div class="field <%= field.dom_id %>">
    <% _saved_object = @object %>
    <% _saved_form = form %>
    <% _saved_field = field %>
    <%= label_tag(field.dom_id, field.label) %>
    <div style="clear:left;padding-left:100px">
    <%= form.fields_for(:material_product_mappings) %>
    <p>
    <%= form.link_to_add('Add Material ', :material_product_mappings) %>
    </p>
    <% @object = _saved_object %>
    <% form = _saved_form %>
    <% field = _saved_field %>
    <% if field.has_errors? %>
    <span class="errorMessage"><%= "#{field.label } #{field.errors.first}" %></span>
    <% end %>
    </div>
    </div>

    # file .../app/views/rails_admin/main/_material_product_mapping_fields.html.erb
    # nested form looks for this file...
    <% ff = f %>
    <% ff.object.errors.full_messages.each do |msg| %>
    <p class="flash_alert_msg"><%= msg %></p>
    <% end %>
    <%= ff.number_field(:num_units, :min => 0.0, :step => 0.01, :size => 6, :style => "text-align:right") %>
    <%= ff.collection_select('material_id',Material.active,:id,:name,:prompt => false) %>
    <%= ff.link_to_remove('Remove') %>

    # also need to copy app/views/layouts/rails_admin/_head.html.haml from the rails_admin installation to
    # your app and modify it to include nested_form's JS. add the following line:

    - head_javascript "nested_form"