Rodrigo
Rodrigo

Reputation: 4802

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!

Upvotes: 13

Views: 2556

Answers (4)

systho
systho

Reputation: 1161

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

Upvotes: 2

Sjors Branderhorst
Sjors Branderhorst

Reputation: 2182

Qumara is right. Still, monkeypatches to the active admin ~ formtastic dsl bridge are possible. What worked for me is opening up the ActiveAdmin::Formbuilder class in the config/initializers/active_admin.rb file. There I added:

class ActiveAdmin::FormBuilder
  include ActionView::Helpers::TagHelper
  def custom_capture_text content
    form_buffers.last << template.content_tag(:li,content.html_safe)
  end

end

Then you can write

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

This worked in activeadmin 0.3 through 0.5. But beware. ActiveAdmin upgrade might break it. Good luck.

Upvotes: 1

R Milushev
R Milushev

Reputation: 4315

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 .

Upvotes: 8

aceofspades
aceofspades

Reputation: 7586

When you call render this actually renders the entire response. Assuming you're trying to nest this, try render_to_string. Although I'm not entirely certain what you have in form_sale and whether that's what you want in that block.

Upvotes: 1

Related Questions