Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

form doesn't work after use render

I trying to use render method inside my activeAdmin form method, but after insert render in code, it stopped to work.

form do |f|
    f.inputs I18n.t('sale_header') do
      f.input :client
      f.input :room
    end

    f.inputs I18n.t('sale_items')  do
      render :partial => "form_sale"
    end

    f.inputs I18n.t('totalization') do
      f.input :sub_total, :input_html => { :disabled => :true }
      f.input :discount
      f.input :total_value, :input_html => { :disabled => :true }
    end

    f.buttons
end

After insert the render method, only form_sale content is showed on screen.

Any help? Thank You!

like image 262
Rodrigo Avatar asked Dec 10 '12 23:12

Rodrigo


2 Answers

According to the documentation , the right way to customize the form in active_admin is :

ActiveAdmin.register Post do
  form :partial => "form"
end

and then in your partial "_form.html.erb" you should use formtastic DSL , something like this:

 <%= semantic_form_for [:admin, @post] do |f| %>
   <%= f.inputs :title, :body %>
   <%= f.buttons :commit %>
 <% end %>

On the web page is clearly stated :

If you require a more custom form than can be provided through the DSL, you can pass 
a partial in to render the form yourself.

which means , that the DSL of active_admin has some slight limitations .

All my experiments with 'render' and 'form :partial' has finished with no result . If you'd like to use partial , it should replace all the form .

like image 165
R Milushev Avatar answered Sep 19 '22 14:09

R Milushev


I'm using form :partial => "form" in a lot of cases and this is definitely the way to go when you want custom forms.

This answer is not here to be accepted as the correct one but sometimes I do not want to make an ERB partial and I just want to add some content to an almost perfect AA generated form.

for those times I use this trick, I add a content method to AA FormBuilder with this initializer :

ActiveAdmin::FormBuilder.class_eval do
  def content
    form_buffers.last << with_new_form_buffer do
        yield
    end
  end
end

And then I can use f.content() in my AA form block :

  form do |f|
    f.content do content_tag(:p, "Hello world!") end
      f.inputs do
        f.input :foo
        f.input :bar
      end
      f.content do content_tag(:p, "Hello world!") end
      f.buttons
    end
like image 24
systho Avatar answered Sep 21 '22 14:09

systho