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(<a href="/projects/1"><img alt="Show" src="/images/icons/show.png?1285276800" /></a> CODE: --Here is my app/views/projects/index.html.erb <title> <p> Projects </p></title> <%= render @projects %> --app/views/projects/_projects.html.erb <div class="project"> <%= project_title_links(project) %> </div> --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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
helpers are automatically scrubbed in rails 3. Try adding .html_safe to the return value On Jan 3, 2011, at 10:10 PM, Ravi Kukreja wrote:> 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(<a > href="/projects/1"><img alt="Show" > src="/images/icons/show.png?1285276800" /></a> > > CODE: > --Here is my app/views/projects/index.html.erb > > <title> <p> Projects </p></title> > <%= render @projects %> > > --app/views/projects/_projects.html.erb > > <div class="project"> > <%= project_title_links(project) %> > </div> > > --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 > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks for your response. I tried adding .html_safe to the following, but its still outputting the same. --app/views/projects/_projects.html.erb <div class="project"> <%= project_title_links(project).html_safe %> </div> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Jan 3, 11:10 pm, Ravi Kukreja <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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(<a > href="/projects/1"><img alt="Show" > src="/images/icons/show.png?1285276800" /></a> > > CODE: > --Here is my app/views/projects/index.html.erb > > <title> <p> Projects </p></title> > <%= render @projects %> > > --app/views/projects/_projects.html.erb > > <div class="project"> > <%= project_title_links(project) %> > </div> > > --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 > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Jan 4, 2011, at 2:23 AM, sameera wrote:> 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 > > On Jan 3, 11:10 pm, Ravi Kukreja <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> 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(<a >> href="/projects/1"><img alt="Show" >> src="/images/icons/show.png?1285276800" /></a> >> >> CODE: >> --Here is my app/views/projects/index.html.erb >> >> <title> <p> Projects </p></title> >> <%= render @projects %> >> >> --app/views/projects/_projects.html.erb >> >> <div class="project"> >> <%= project_title_links(project) %> >> </div> >> >> --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 >> >> -- >> Posted viahttp://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Perfect - that did it. Thank You so much for your help! I appreciate it. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.