Problem with using the REST-ful link_to while using it in a mailer
-
27-09-2019 - |
문제
I strongly feel its a silly error and I'm somehow not able to see through it. I'm trying to use this piece of code in the view of my mailer.
<p><%= link_to 'here', unsubscribe_path(:email => "a@b.com") %></p>
And I've defined a named route in my routes file :
map.unsubscribe '/unsubscribe', :controller => :users, :action => :unsubscribe
So when I send my email this is the link I get:
http://b.comonly_pathtruecontrollerusersactionunsubscribe/
Any ideas ? Thanks !
EDIT
Here's the view:
<html>
<body>
<p>
Hi, <br />
You have an alert for : <%= @alert.name %>. <br />
Here is some brief information about it: <br />
<%= @alert.details %><br /><br />
<br />Thank You,<br />
</p>
<p>
Click <p><%= link_to 'here', unsubscribe_path(:email => "a@b.com") %></p> to unsubscribe</p>
</body>
</html>
And here's the mailer:
class Alert < ActionMailer::Base
def send(alert, email)
@subject = "hi"
@recipients = "xyz@mail.com"
@from = "xyz@mail.com"
@sent_on = Time.now
@content_type = "text/html"
@alert = alert
@email = email
end
end
해결책
Here's an example of a working template, I can't say for sure why yours isn't working but try generating the url in the notifier.
# notifier
def new_message_email(response)
I18n.locale = response.recipient.language
subject I18n.t(:you_have_received_new_message_from_sender, :sender => response.sender.login)
from "info@domain.com"
recipients response.recipient.email
content_type "text/html"
sent_on Time.now
body :sender_login => response.sender.login, :recipient_login => response.recipient.login
end
#template
= word_wrap "Hi #{@recipient_login}."
= word_wrap ""
%p
= word_wrap "You have received a new personal message from #{@sender_login}.", :line_width => 60
= word_wrap "Click #{link_to 'here', account_inbox_url} to view your domain.com message inbox.", :line_width => 60
= word_wrap "If the above URL does not work try copying and pasting it into your browser. If you continue to have problems, please feel free to contact us at support@domain.com.", :line_width => 60
You will need to generate a url, rather than a path.
eg:
account_path => /account
account_url => http://domain.com/account
edit: this should work:
#notifier
body :unsubscribe_url => unsubscribe_url(:email => "a@b.com")
#template
= word_wrap "Click #{@unsubscribe_url} to unsubscribe.", :line_width => 60
(see above example for how this all fits together)
다른 팁
Does this unsubscribe_url (or _path) work in any other view?
The returned value looks like a something made from a Hash converted to String. Try to call .inspect on the object returned by "link_to()" - maybe there will be some clue?
Maybe something redefined a link_to method somewhere in your code? Use "grep def.*link_to" - maybe some clues will be there?
A test may be useful in this and later cases. Here is an example (however this is what I use in rails 1, but I hope this does not differ too much in your rails version).
def test_routing
assert_routing '/unsubscribe', {:controller => 'user', :action => 'unsubscribe'}
assert_recognizes Hash[:controller => 'user', :action => 'unsubscribe'],
{:path=>'/unsubscribe', :method => :get},
{"email"=>"a@example.com"}
# ..and so on
end