Reputation: 17631
I am wondering how I can handle this:
I want to generate a link_to into a body message when I save it through a model method.
Let say in this method, I create a new Message and wants to put a link inside it so when users of my website could click on it when going through every messages.
How would you do this in a nice way ? I tried this, but didn't work
Request.rb
# Create a new Message after an acceptance
def notify_acceptance
msg = Message.new(subjects: [self.subject], author: self.evaluated_by)
msg.body = "a warm welcome to #{ActionView::Helpers.url_to_user_profile(self.requested_by)} who just joined us!"
msg.distribute([self.subject], self.evaluated_by)
return msg.save!
end
Here my Helper file :
module RequestsHelper
def url_to_user_profile(user)
link_to user.name, profiles_path(user.id)
end
end
Thx !
Upvotes: 1
Views: 10279
Reputation: 1757
I think the problem is your model(maybe user) did not recognize profiles_path(user.id), so add following in User model
include ActionDispatch::Routing::UrlFor
include Rails.application.routes.url_helpers
I did not suggest include more view helpers in your model, you can't use link_to, but path helper method works, then create url like " link_name " in your msg It works in Rails3.
Upvotes: 0
Reputation: 3773
Have you tried this way?
link_to user.name, :controller => "profiles", :action => "show", :id => user.id
so:
link_to "String you want", :controller => "controllers name", :action => "action", :(param name) => value
and here you can insert how many params do you need
EDIT
with profiles_url(user.id)
you get the url to this page, with that you can manualy create your linklike this:
msg.body = "a warm welcome to <a href='#{profiles_url(self.requested_by.id)}'>#{profiles_url(self.requested_by.name)}</a> who just joined us!"
Upvotes: 1
Reputation: 42863
The simple answer is that you cannot. link_to
is created from ActionPack
where as models inherit from ActiveRecord
.
A simple way around this is putting your link_to
logic in your helpers
.
For example:
app/views/index.html.erb
link_to_home
app/helpers/application_helper.rb
def link_to_home
if root_url?
"Home"
elsif params[:controller] = "posts" && params[:action] == 'index'
"Home"
else
link_to "Home", root_url
end
end
This way if you have pagination on the index page and that is your root url it will not link to Home. But if you used link_to_unless_current
it will link to home even though it is the same controller and action that execute link_to_unless_current
. Doesn't really matter if this example doesn't make sense. It's the rails way to keep link_to
logic in helpers.
If it is really necessary you could just create a string in your model that has regular HTML and then escape that in the view.
class Post < ActiveRecord::Base
def link
"<a href="#{url}">#{title}</a>"
end
end
Then in your view you could have <%= @post.link %> and it would link to url
and it would be called title
assuming those were attributes on your @post
object and you had something saved
Upvotes: 3