How to pass value in a link_to inside a mail of Rails 3.1?

Go To StackoverFlow.com

1

This is the way I am approving certain events in my site. The idea is: With every new event, an email is send to "admin@example.com". The email contains certain stuff and a link to a method that changes the 'approved' boolean. Everything works apart from the url generated in the email.
It generates, http://example.com/approve_event . And I get the error 'Could not find an event with id nil'

How should I do to get a url that points to something like http://example.com/approve_event?=23

AdminNotifier

  def approve_event(event)
    @event = event    
    mail(:to => "admin@example.com", :subject => "Event pending of approval")
  end

email view html

  = link_to "Approve", approve_event_url(:id=> @event.id)

EventController

 def approved
   @event = Event.find(params[:id])
   @event.approved = true
   @event.save
 end
2012-04-04 17:13
by Sergio Nekora
how are you retrieving @event in the mailer - alf 2012-04-04 17:20
I call the mailer from the EventController. AdminMailer.approve_event(@event).deliver

Shall I have in the mailer method something like: @event = Event.find(params[:id] - Sergio Nekora 2012-04-04 18:01

You're using @eventin the view, and for that to work you need to define the @event variable in the mailer. Take a look at the approve_event method and check if you're using a valid event object - alf 2012-04-04 18:14
Yeah!! The object I was passing did not have id yet. Thanks guys, you are great - Sergio Nekora 2012-04-04 18:44
Cool! I just added this as an answer so you can mark it and close the question. Cheers - alf 2012-04-04 19:02


0

You need to make sure that the @event variable is defined correctly in the mailer method (approve_event in this case), to make sure that it will be available in the view.

More info on the Rails guides

2012-04-04 19:01
by alf
Ads