Hi everyone,
Like so many other here, I too am new to Rails (and Ruby), and so far
it’s been a great experience. I haven’t been able to get around one
problem though. When trying to use the mailer, I recieve an error like
this:
ActionView::ActionViewError in Tell_friend#send_email
No rhtml, rxml, or delegate template found for send_to_friend
The scenario is this:
A form on a page is submitted to a controller “tell_friend”, action
“send_email”.
In tell_friend_controller.rb’s send_email(), mail is sent like this:
TellFriend.deliver_send_to_friend(params[:address])
The model, tell_friend.rb, contains this:
class TellFriend < ActionMailer::Base
def send_to_friend(address)
# Email header info MUST be added here
@recipients = address
@from = [email protected]
@subject = "Hello World"
# Email body substitutions go here
@body["address"] = address
end
end
Now, send_to_friend.rhtml is present in views/tell_friend/. But the
error message would seem to indicate that it is not the case.
class TellFriendController < ApplicationController
def send_email
flash[:email_notice] = “E-mail sent to
#{params[‘address’]}.”
TellFriend.deliver_send_to_friend(params[:address])
# Go back to the post they were viewing
redirect_to :controller => ‘view’, :action =>
‘view_post’, :params => {‘id’ => params[:id], :address =>
params[:address] }
end
end
And here’s the mailer code:
class TellFriend < ActionMailer::Base
def send_to_friend(address)
@recipients = address
@from = [email protected]
@subject = "Hello World"
# Email body substitutions go here
@body["address"] = address
end
end
Also, if anyone has any unrelated advice on the above code I’d love to
hear it too.
Thanks again,
Doug
Steven S. wrote:
Can you post all the code in tell_friend_controller.send_email() as
well as any other code in tell_friend.rb (the mailer)?
I think you’ve pretty much done things the right way. I’ve heard of
a couple of problems when there are extraneous views in the directory
where the mail template is (app/views/tell_friend). As far as the
mailer goes, I believe the only one you need is send_to_friend.rhtml
so I’d remove the others from that directory if possible. Also,
while it should make no difference, can you change your code in
tell_friend_controller.send_email to something like the following
just to see if a different error is produced:
Also, you may want to consider using a different name for your mailer
(such as TellFriendMailer) as there may be some sort of conflict
with other views used in conjunction with the TellFriendController
(which may be why there are other views in apps/views/tell_friend).
If you do make this change, then you’d need to move
send_to_friend.rhtml to the app/views/tell_friend_mailer directory.
I think you’ve pretty much done things the right way. I’ve heard of
a couple of problems when there are extraneous views in the directory
where the mail template is (app/views/tell_friend). As far as the
mailer goes, I believe the only one you need is send_to_friend.rhtml
so I’d remove the others from that directory if possible. Also,
while it should make no difference, can you change your code in
tell_friend_controller.send_email to something like the following
just to see if a different error is produced:
Also, you may want to consider using a different name for your mailer
(such as TellFriendMailer) as there may be some sort of conflict
with other views used in conjunction with the TellFriendController
(which may be why there are other views in apps/views/tell_friend).
If you do make this change, then you’d need to move
send_to_friend.rhtml to the app/views/tell_friend_mailer directory.
Hi,
I tried your suggestions twice, still no luck. Pretty bizarre. I think
I’ll just outsource the emailing to a perl script for the time being,
until I figure it out.
Hi ,
I was facing similar problems but I was able to send an email using
sendmail finally. I created a directory called
my_mailer in {RAILS_ROOT}/apps/views/test/
MyMailer defined in my_mailer.rb inside the models directory
<<<<<<<<
class MyMailer < ActionMailer::Base
def sendmail (recipient)
recipients recipient
subject “test mail”
from “[email protected]”
body = { :text => “First mail” }
end
end
<<<<<<Controller code
class TestController < ApplicationController
def index @message = ‘riding on rails’
end
def mail
MyMailer::deliver_sendmail ( params[:to] )
render :text => ‘mail sent’
end
end
Does rails require the name to be my_mailer ? and should this have to be
inside models ? Cant we place it inside the controllers? I feel thats a
more
natural place
Also the template has to be named sendmail.text.plain.rhtml in the
my_mailer directory
The question I have is what exactly needs to go into the view ? I had
just
one line
<%= @body[:text] %>
But I get the error
ou have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]