Search code examples
ruby-on-railsstringruby-on-rails-4hyperlinklink-to

Rails 4: Embed link in string


I have a string field in one of my controllers which is a message. I want to embed a clickable link in it.

Something like this works in the view:

<% dd = "Click on link to #{link_to 'View ', "www.google.com"} asdad"  %>
<%= dd.html_safe %> 

but in the controller its not possible to store dd and I get this error:

NoMethodError (undefined method `link_to' for ...

I tried also to_s :

<% dd = "Click on link to #{link_to 'View ', "www.google.com"} asdad".to_s %>  
Message.create(:user_id => current_user.id, :cnt => dd)

but still the same error. Please, help me to know how can I embed a link into string in rails 4.


Solution

  • Try

    in the controller code

    view_context.link_to '...', '...'