Bryce
Bryce

Reputation: 2872

Rails add partial to Simpleform custom inputs

I'm creating a custom SimpleForm input. It utilizes a javascript modal popup to select values.

I can basically get it to work if I include the modal partial in the form view, but I would ideally like to have the custom input push the partial to the template.

However, I can't get it to work. I'm trying this, but it doesn't render the partial correctly.

class ProductCategoryImageSelectInput < SimpleForm::Inputs::CollectionSelectInput
  def input

  html = ''
  html << '<a href="#myModal" role="button" data-toggle="modal">Launch demo modal</a>'

  File.open("#{Rails.root}/app/views/product_categories/_browse_modal.html.erb", 'r') do |f|
    html << f.read
  end

  end
end

Any thoughts?

EDIT

I'm apparently too new of a user to post screenshots, but here are links to the differences in functionality:

When I embed the modal code in the form, I get this, which is the desired functionality: working

When I try to put the modal code in the custom input, I get this instead: not working

Upvotes: 0

Views: 905

Answers (2)

tabularasa
tabularasa

Reputation: 61

You can also render partial from a custom input using the SimpleForm::FormBuilder instance which is assigned to an instance variable named @builder:

class ProductCategoryImageSelectInput < SimpleForm::Inputs::CollectionSelectInput
  def input
    @builder.template.render(partial: 'product_categories/browse_modal', locals: { foo: 'bar'})
  end
end

Upvotes: 6

Pedro Nascimento
Pedro Nascimento

Reputation: 13926

Weirdly, it seems I've already answered it here. Does this work for you?

ERB.new(f.read).result(binding)

PS: Updated with ERB instead of HAML.

Upvotes: 0

Related Questions