Forum: Ruby on Rails can't find view from Mailer?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Da45ca349d8ded3246b3e57f17274e25?d=identicon&s=25 Doug (Guest)
on 2005-12-24 02:14
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 = xx@xxx.com
                @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
4353600bbb6b6e0950e62f7f44e49dd5?d=identicon&s=25 Steven Smith (Guest)
on 2005-12-24 04:23
(Received via mailing list)
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
26a9fa2376684a5e973ffee20dd03b58?d=identicon&s=25 Doug (Guest)
on 2005-12-24 17:35
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 = xx@xxx.com
                @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 Smith 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
4353600bbb6b6e0950e62f7f44e49dd5?d=identicon&s=25 Steven Smith (Guest)
on 2005-12-24 20:12
(Received via mailing list)
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.
4353600bbb6b6e0950e62f7f44e49dd5?d=identicon&s=25 Steven Smith (Guest)
on 2005-12-24 20:18
(Received via mailing list)
Also, what is in the directory  app/views/tell_friend/?

Thanks.
Da45ca349d8ded3246b3e57f17274e25?d=identicon&s=25 Doug (Guest)
on 2005-12-24 21:03
Steven Smith 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:in
`render_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:in
`render'
/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:in
`create!'
/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:in `new'
/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:in
`method_missing'
#{RAILS_ROOT}/app/controllers/tell_friend_controller.rb:5:in
`send_email'

What do you think?
Thanks, Doug
4353600bbb6b6e0950e62f7f44e49dd5?d=identicon&s=25 Steven Smith (Guest)
on 2005-12-24 22:01
(Received via mailing list)
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.
Da45ca349d8ded3246b3e57f17274e25?d=identicon&s=25 Doug (Guest)
on 2005-12-24 22:44
Steven Smith 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
6828ffc79486cd2442714bf32286a910?d=identicon&s=25 Vivek Krishna (Guest)
on 2005-12-30 15:02
(Received via mailing list)
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  "vivek@localhost.com"
    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
D51a24ae1017f53158d70b621494255d?d=identicon&s=25 Bontina Chen (abon)
on 2007-02-24 04:32
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
D51a24ae1017f53158d70b621494255d?d=identicon&s=25 Bontina Chen (abon)
on 2007-02-24 04:52
Bontina Chen 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.
This topic is locked and can not be replied to.