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.
我想将内容“来自#{sender.fullname}的新消息”全部链接到converstations_path。我尝试了以下内容。
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?
结果是它将呈现引号内的所有内容,忽略html或link_to帮助器。我该怎么做这个链接?
I am rendering the notification.content inside of my _notification.html.erb this is what the file looks like
我在_notification.html.erb中呈现notification.content这就是文件的样子
<strong><%= notification.content %></strong>
<span class="pull-right"><%= notification.created_at.to_formatted_s(:short) %></span>
2 个解决方案
#1
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.
解决此问题的一种方法是在通知模型中添加一个字段,例如src。此src将是您要将用户发送到的链接。然后,每当您向用户呈现此通知时,您都可以使用link_to中的notification.src。
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.
这将是我推荐的方法,但我相信你也可以使用notification.content.html_safe之类的东西来解析+渲染HTML而不是仅渲染它。
#2
-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 %>
#1
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.
解决此问题的一种方法是在通知模型中添加一个字段,例如src。此src将是您要将用户发送到的链接。然后,每当您向用户呈现此通知时,您都可以使用link_to中的notification.src。
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.
这将是我推荐的方法,但我相信你也可以使用notification.content.html_safe之类的东西来解析+渲染HTML而不是仅渲染它。
#2
-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 %>