Hello:
I am learning to use Helpers in Rails 3.
I have a project model with ‘Name’ attribute in it. I want to display
all the Projects with a ‘Show’ link next to it. The Problem is instead
of displaying a link for ‘Show’ it is displaying the html(
CODE:
–Here is my app/views/projects/index.html.erb
Projects
<%= render @projects %>
–app/views/projects/_projects.html.erb
<%= project_title_links(project) %>
–app/helpers/people_helper.rb
module PeopleHelper
def project_title_links(project)
content_tag :h1 do
[ project.title,
link_to_icon(‘show’, project)
].join(’ ')
end
end
end
–app/helpers/application_helper.rb
module ApplicationHelper
def link_to_icon(icon_name, url_or_object, options={})
link_to(image_tag(“icons/#{icon_name}.png”),
url_or_object,
options)
end
end
helpers are automatically scrubbed in rails 3. Try adding .html_safe to
the return value
Thanks for your response. I tried adding .html_safe to the following,
but its still outputting the same.
–app/views/projects/_projects.html.erb
<%= project_title_links(project).html_safe %>
Hi Ravi,
Its because with rails3 they have implemented script safety by
default. If you want to print html/script codes you need to
explicitly declare it. try this
<%= raw project_title_links(project) %>
should work
cheers
sameera
Although this will work, it seems like bad form to do this for helpers
in the view.
IMO, if the helper is intended to return renderable html, it should.
Simply add .html_safe to
the end of .join() in your helper --> .join(’ ').html_safe
Hope this helps
Perfect - that did it. Thank You so much for your help! I appreciate it.