Ruby on Rails Monday, September 29, 2014

Glad to see it working. Happy Coding :) 

On Mon, Sep 29, 2014 at 12:51 PM, Paolo Wang <paulwangzy@gmail.com> wrote:
I finally got it working in ActiveAdmin without a custom form. I'll post how I did it for references if somebody need.
First I set my custom attributes in the LineItem model:

class LineItem < ActiveRecord::Base
attr_accessor :source
attr_accessor :target

(it doesn't work adding :source and :target to the ActiveAdmin file under permit_params, I get an error that there are no column with :source/:target if not added to the model with attr_accessor)

ActiveAdmin.register Order do

  permit_params :id, :title,
                line_items_attributes: [:id, :order_id, :language_pair_id, :source, :target]

  form do |f|
    f.inputs do
      f.input :title
      f.has_many :line_items, :allow_destroy => true do |cf|
        cf.input :source, :collection => Language.pluck(:lang)
        cf.input :target, :collection => Language.pluck(:lang)
      end
    end
    f.actions
  end

  controller do
    def create
      @order = Order.create(:title => params[:order][:title])
      params[:order][:line_items_attributes].each do |x, y|
        source = Language.find_by_lang(y[:source])
        target = Language.find_by_lang(y[:target])
        pair = LanguagePair.find_by(:source_id => source.id, :target_id => target.id)
        @order.line_items.create(:language_pair => pair)
      end
      create!
    end
  end
end

it work flawlessly.

Thank you all for the help.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8ee16610-4b5b-42a6-9327-7098a42e69df%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAFKVRj-KNFSgtmwJ0Aw3NEt-8_tVwBcN5ps0NMei%2BeHSZ3WJOw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment