NoMethodError: undefined method `deliver_now' - ruby-on-rails

I have this in my controller:
def create
#user = User.new(user_params)
if #user.save
UserMailer.account_activation(#user).deliver_now
....
end
And I keep getting this error:
NoMethodError: undefined method `deliver_now' for #<ActionMailer::MessageDelivery:0x007ffa6646eb60>
Any ideas why it's not finding the deliver_now method?

deliver_now has been introduced in rails 4.2.beta2. You are probably using earlier version.

I think it's because it's deliver (or deliver! for the bang version) and not deliver_now.
See ActionMailer::Base

deliver works instead of deliver_now for older versions of Rails.

Related

undefined method `model_name' for TrueClass:Class

Im getting this error after updating a has_one field
undefined method `model_name' for TrueClass:Class
This is the code, on after_sign_up of Devise
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
#dash = Dashboard.create(name: "David")
#user = current_user
#user.dashboard_id = #dash.id
#user.save
end
end
The code works, but give the error in the end.
The error message is a bit misleading. after_sign_up_path_for expects you to return a path. The last statement in your method (#user.save) returns a boolean. The error should disappear if you add a (return) statement at the end of your method to provide the path.

undefined method for Questions

I am getting the error undefined method `questions' for nil:NilClass when viewing different profiles. I am not seeing why this is an undefined method. Anyone have a clue what I did not do correct?
Logs:
NoMethodError (undefined method `questions' for nil:NilClass):
app/controllers/users_controller.rb:80:in `show'
Users Controller:
def show
#user = User.find_by(username: params[:id])
The error is letting you know that the #user variable is not being set. Check your before_action/before_filter or whatever is setting that variable to make sure it is not failing.

Rapidfire and Devise

I implemented Devise and went through the instructions to install Rapidfire
and for the application controller I have
def current_user
current_user #rb:7
end
def can_administer
true # just for right now...
end
but on the page I get a something went wrong and if I look in the console it says
ActionView::Template::Error (stack level too deep)
app/controllers/application_controller.rb:7
which is the current_user line.
Can anyone tell me whats going on?
You are creating a method called current_user, and you are returning the value of current_user.
Ruby doesn't require that you use parenthesis when calling methods.
current_user
is the same thing as
current_user()
You are calling the current_user function over and over and over again.
There is no reason for you to define a method called current_user, when devise gives that to you.

Send an actionmailer email upon user registration in devise rails 3 application

I'm trying to send an email upon user registration in a rails 3 application that is using the devise gem. Every time I try to the send the email I get the following error:
NoMethodError in Devise::RegistrationsController#create
undefined method `welcome_email' for UserMailer:Class
My app/model/user.rb has the following code...
after_create :send_welcome_email
def send_welcome_email
UserMailer.welcome_email(self).deliver
end
My app/mailers/user_mailer.rb has the following code...
class UserMailer < ActionMailer::Base
default from: "support#mysite.com"
def welcome_email(user)
#user = user
#url = "http://mysite.com/login"
mail(:to => "#{user.email}", :subject => "Welcome to My Awesome Site")
end
end
The method welcome_email exists so I'm not sure why I'm getting the error. Been trying to resolve this problem for the past couple of hours.
Thanks in advance for you help!! As always if you give me a good answer I will accept it as so.
Alex
Looks like the welcome_email is an instance method. And it looks like the error says it needs to be a class method. So try rewriting the method declaration as:
def self.welcome_email(user)
I guess that should solve it.

Active admin layout was lost after overriding Controller action

My new controller action:
controller do
layout 'active_admin'
def index
#pages = Page.all
end
end
After refresh the page i received:
undefined method `base' for nil:NilClass
render view_factory.layout
What should I do for fixing this?
I start rewriting controller action because i received this message for my index action:
undefined method `num_pages' for #<Array:0x0000000b860eb0>
render renderer_for(:index)
Maybe anyone know how fixing this?
The initial undefined method 'num_pages' for #<Array:0x0000000b860eb0> may be occurring if you have an instance variable set in a before_filter in ApplicationController with the plural name of a model as I did. The bug is reported here.
Would need to see the code on the view page for this but it sounds to me like you are making a call for num_pages on an object that is an array class. Since Ruby's array class has no num_pages method, it is throwing an error.
Jamie you are right! But then i received this message for my index action:
undefined local variable or method `per' for ActiveRecord::Relation
And I fix this problem by doing this:
# config/initializers/will_paginate.rb
if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
alias_method :per, :per_page
alias_method :num_pages, :total_pages
end
end
end
end
The bug is reported here.

Resources