1

I have a rails helper method that is called by:

<%= link_to_add_fields "Add Product", f, :products %>

in application_helper.rb:

def link_to_add_fields(name, f, association)
  new_object = f.object.send(association).klass.new
  id = new_object.object_id
  fields = f.fields_for(association, new_object, child_index: id) do |builder|
     render(association.to_s.singularize + "_fields", f: builder)
  end
  "<div>BLAHBLAHBLAH</div>" +
  link_to(name, '#', class: 'add_fields', data: {id: id, fields: fields.gsub('\n', '')})+ ""
end

But this returns everything visible as text on the web page. How do I return both the div and the link_to, so they are both rendered correctly?

1 Answer 1

2

Try something like this instead:

"<div>BLAHBLAHBLAH</div> #{link_to(name, '#', class: 'add_fields', data: {id: id, fields: fields.gsub('\n', '')})}".html_safe

Note the use of html_safe. Remember that Rails will write your unescaped HTML code as a plain 'ol string to help prevent issues like Html Script Injection. That's what you were encountering, so you simply need to ensure it is written as actual HTML code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.