Can't find view from Mailer?

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.

Any ideas?

Thanks,
Doug

Can you post all the code in tell_friend_controller.send_email() as
well as any other code in tell_friend.rb (the mailer)?

Thanks.
Steven

Hi Steven,

Here’s the controller code:

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)?

Thanks.
Steven

Quick question, what directory does tell_friend.rb (the mailer)
reside in?

Also, do you have any sort of stack trace from when the error occurs?

Thanks.

Also, what is in the directory app/views/tell_friend/?

Thanks.

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:

email = TellFriend.create_send_to_friend(params[:address])
TellFriend.deliver(email)

instead of

TellFriend.deliver_send_to_friend(params[:address])

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.

Steven S. wrote:

what directory does tell_friend.rb (the mailer) reside in?
tell_friend.rb is in models/

Also, what is in the directory app/views/tell_friend/?
deliver_send_to_friend.rhtml
send_to_friend.rhtml
send_email.rhtml

I tried a few filename combos trying to figure out what it’s looking
for.

Finally, the stack trace:

/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.0/lib/action_view/base.rb:282:in
pick_template_extension' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.0/lib/action_view/base.rb:196:inrender_file’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.0/lib/action_view/base.rb:227:in
render' /usr/lib/ruby/gems/1.8/gems/actionmailer-1.1.3/lib/action_mailer/base.rb:357:inrender’
/usr/lib/ruby/gems/1.8/gems/actionmailer-1.1.3/lib/action_mailer/base.rb:352:in
render_message' /usr/lib/ruby/gems/1.8/gems/actionmailer-1.1.3/lib/action_mailer/base.rb:300:increate!’
/usr/lib/ruby/gems/1.8/gems/actionmailer-1.1.3/lib/action_mailer/base.rb:263:in
initialize' /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:18:innew’
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:18:in new' /usr/lib/ruby/gems/1.8/gems/actionmailer-1.1.3/lib/action_mailer/base.rb:223:inmethod_missing’
#{RAILS_ROOT}/app/controllers/tell_friend_controller.rb:5:in
`send_email’

What do you think?
Thanks, Doug

Steven S. wrote:

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:

email = TellFriend.create_send_to_friend(params[:address])
TellFriend.deliver(email)

instead of

TellFriend.deliver_send_to_friend(params[:address])

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.

Thanks for your help!
Doug

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

ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.template_root = “app/views/test”

<<<<<<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.[]

Any idea how I can access the body etc?

Thanks
Vivek

I tried to create a dir under apps/views/test/mymailer

but it still can’t find the template.
Anyone know any other way to fix it?

THANKS

Bontina C. wrote:

I tried to create a dir under apps/views/test/mymailer

but it still can’t find the template.
Anyone know any other way to fix it?

THANKS

I found the possible solution.
Just add
require ‘action_mailer’
in the yourmailer.rb

It works for me.
Hopw this helps.