views:

52

answers:

2

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 => "[email protected]") %></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 => "[email protected]") %></p> to unsubscribe</p>
</body>
</html>

And here's the mailer:

class Alert < ActionMailer::Base

def send(alert, email)
  @subject = "hi"
  @recipients = "[email protected]"
  @from = "[email protected]"
  @sent_on = Time.now
  @content_type = "text/html"

  @alert = alert
  @email = email
end

end

+1  A: 

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         "[email protected]"
    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 [email protected].", :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 => "[email protected]")

#template
= word_wrap "Click #{@unsubscribe_url} to unsubscribe.", :line_width => 60

(see above example for how this all fits together)

mark
+1  A: 

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"=>"[email protected]"}
  # ..and so on
end
Arsen7