0
votes

Many simmilar Q/A on this topic here and there, but I was unable to find exact solution for my problem. Using Rails 3.0.9 now, and trying to upgrade existing older application(not Rails). The goal is to send simple email to new clients created by admins. Have been following this oficial guide (and many others), but with no success.

The issue is, that method(s) defined in this controller, from class 'UserMailer', aren`t recognised from another controller, while class 'UserMailer' itself recognised is(how do I know this, will be explained below):

/app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  default :from => "[email protected]"

  def kokotina # << this is just a dummy method for testing
    caf = "ssss"
  end

  def regMailUsr(nazov, priezvisko, email, pass)
    @nazov = nazov
    @priezvisko = priezvisko
    @email = email
    @pass = pass
    @url = "http://loyalty2.xxxx.sk"
    mail(to: email, subject: 'Vaša registrácia bola dokončená.')
  end
end

I have also created View for this mail controller but that is not important right now.

The fragments from clients controller are here:

/app/controllers/clients_controller.rb

# encoding: UTF-8

class ClientsController < ApplicationController
  load_and_authorize_resource
.......
  def new
    @noveHeslo = genHeslo(10) # << I defined this in application_controller.rb and it works
    UserMailer.kokotina # << just a dummy method from UserMailer
    @client = Client.new(params[:client])
    .......
  end
.......
  def create
    .......
    if @client.save
      #send email to new client:
      UserMailer.regMailUsr(params[:client][:fname], params[:client][:lname], params[:client][:email], params[:client][:password]).deliver
      .....
  end ......

Now how do I know that my class is loaded? If in client controller, I change 'UserMailer' to 'xUserMailer', I will get 'no class or method in ...' error, but without 'x', I get only:

'undefined method `kokotina' for UserMailer:Class'

I also tried to define my methods in UserMailer:Class like this:

def self.kokotina # << this is just a dummy method for testing
  caf = "ssss"
end
#or even like this
def self <<
  def kokotina # << this is just a dummy method for testing
    caf = "ssss"
  end
end
#and then tried to invoke this method(s) like this:
UserMailer.new.kokotina
#or simply
kokotina

Strange is, that when I put contents of file '/app/mailers/user_mailer.rb' at the end of 'application_helper.rb' file, just after the end of 'module ApplicationHelper', I get no errors but of course, it won`t work.

Please keep in mind that I have no problem coding in another languages, but this mystic/kryptic rules of Ruby on Rails are still a complete mistery to me and unfortunatelly, I don`t have time or even motivation to read time extensive quides or even books for RoR beginners. I have been coding much more difficult applications and implementations, but this heavily discriminating system is driving me nuts.

Thank you all!

1

1 Answers

0
votes

Problem solved! The trick was, that in '/app/mailers/user_mailer.rb', I had multibyte characters. In mail subject. So I added:

# encoding: UTF-8

at the very first line of '/app/mailers/user_mailer.rb'

I found this by total accident: later my rails app could not start, and server was simply throwing HTTP 500 error. So no trace, error defining etc.

I found out that multibyte string in: mail(to: email, subject: 'Vaša registrácia bola dokončená.')

Was responsible for crash. When I removed that string, I noticed one important side effect: my methods became magicaly available for another controller!!!!

So if someone could give me at least one reason to lowe Rails...