0

I have a notices model which contains records of notices for each user. Each notice record contains a message which is a text field with the notice message. These messages look like:

"#{current_user.username} liked your photo '#{@photo.name}."

In the example above, I would like the user and the photo to also be hyperlinks to that user and photo.

Here is a snippet from my likes_controller which generates a notice when a new like is created:

class LikesController < ApplicationController

  def create
    @photo = Photo.find(params[:id])
    @like = Like.new(:photo_id => @photo.id, :user_id => current_user.id)

    if @like.save
      @notice = Notice.new(:user_id => @photo.user_id, :message => "#{current_user.username} liked your photo '#{@photo.name}'
    end

Any thoughts on how I can include links in the message; is this even possible? Thanks.

Tony Beninate
  • 1,926
  • 1
  • 24
  • 44
  • 1
    http://stackoverflow.com/questions/1332013/what-approach-do-you-take-for-embedding-links-in-flash-messages this is a method. Not sure I like it but it works. You can also search for "include helpers in controllers". – Robin Jan 14 '12 at 14:12
  • Thanks Robin, the first answer on there does the trick for me. I was hoping for a cleaner method, but it works for now. – Tony Beninate Jan 14 '12 at 14:23

1 Answers1

2

Adding a link to your message is a rendering issue. In my opinion, you're rendering the message too soon, I would render it in the view.

If you change your Notice model so that it contains a the user_id and the like_id, you can render the notice text in the view (which also lets you localize the text later, should it prove necessary).

Rendering in the view lets you use the standard link_to helper to generate your links.

Jeff Paquette
  • 7,089
  • 2
  • 31
  • 40