1

I have a method inside of my message model for creating a notification and when the notification is created I'm also creating the contents for a string variable content. This is what the method looks like.

 def create_notification

  if self.conversation.sender_id == self.user_id
    sender = User.find(self.conversation.sender_id)
    Notification.create(content: "New message from #{sender.fullname}", user_id: self.conversation.recipient_id)
  else
    sender = User.find(self.conversation.recipient_id)
    Notification.create(content: "New message from #{sender.fullname}", user_id: self.conversation.sender_id)
  end
end

I would like to make the content "New message from #{sender.fullname}" all a link to converstations_path. I have tried the following.

        Notification.create(content: "<a href="/converstions">New message from #{sender.fullname}</a>", user_id: self.conversation.recipient_id)

also

        Notification.create(content: "<%= link_to 'New message from #{sender.fullname}', conversations_path %>", user_id: self.conversation.recipient_id)

The result is it will render everything inside of the quotation marks ignoring the html or the link_to helper. How can I make this a link?

I am rendering the notification.content inside of my _notification.html.erb this is what the file looks like

<strong><%= notification.content %></strong>
<span class="pull-right"><%= notification.created_at.to_formatted_s(:short) %></span>
G. Mac
  • 13
  • 3

2 Answers2

0

One way to solve this is to add a field to your notification model, such as src. This src would be the link that you would like to send the user to. Then, whenever you are rendering this notification to the user, you can use the notification.src in your link_to, instead.

This would be my recommended approach, however I believe you could also use something like notification.content.html_safe in order to parse+render the HTML instead of just rendering it.

JGrishey
  • 75
  • 5
-1

I tried to solve this issue by referring this page

(Model)sender = User.find(self.conversation.sender_id)

link = "<a href ='URL'>New message from #{sender.fullname}</a>"

Notification.create(content: "#{link}", user_id: self.conversation.recipient_id)

(View)<%= notification.content.html_safe %>
eisbehr
  • 12,243
  • 7
  • 38
  • 63