undefined method `default' for ActionMailer - ruby-on-rails

I'm having this weird error with Rails 5 action mailer.
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "from#example.com"
layout 'mailer'
end
irb
~/workspace/rails5 > rails c ruby-2.4.1#rails5
Running via Spring preloader in process 45498
Loading development environment (Rails 5.1.2)
2.4.1 :001 > ApplicationMailer
NameError: uninitialized constant Mail::TestMailer
from app/mailers/application_mailer.rb:1:in `<top (required)>'
from (irb):1
2.4.1 :002 > ApplicationMailer
NoMethodError: undefined method `default' for ApplicationMailer:Class
from app/mailers/application_mailer.rb:3:in `<class:ApplicationMailer>'
from app/mailers/application_mailer.rb:1:in `<top (required)>'
from (irb):2
2.4.1 :003 >
This is the default Scaffold of rails 5.
Two really weird things happen the first is that it always tells me the uninitialized constant then trows default is not a method right after.
Just wondering how to fix it or if I should downgrade to Rail 5.0

The solution turned out to be that if you have a model named mail the ActionMailer will grab that one first instead of the rails class. If this happens then it fails the first time but the second time will load the rails class and try and smash them together. Allowing the submodule to work but treating the main class as a monkey path.

Related

How to add iframe to sanitized tags in ruby on rails 4.2.6?

I am trying to add iframe to santized tags by using
config.action_view.sanitized_allowed_tags
I tried to find what tags are already allowed by using the console.
uraai#raiuorial:~/workspace/corse (master) $ heroku run rails c
Running rails c on ⬢ fa4... up, run.9396
Loading production environment (Rails 4.2.6)
irb(main):001:0> puts helper.sanitized_allowed_tags.to_a
NoMethodError: undefined method `sanitized_allowed_tags' for #<ActionView::Base:0x007f18ea91ea60>
from /app/vendor/bundle/ruby/2.3.0/gems/metamagic-3.1.7/lib/metamagic/view_helper.rb:30:in `method_missing'
from (irb):1
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
from /app/bin/rails:8:in `require'
from /app/bin/rails:8:in `<main>'
irb(main):002:0>
Any idea how to add it without ignoring the other tags? Thanks
Please have a look on the next example:
module Tapp
class Application < Rails::Application
# In config/application.rb
config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a', 'br', 'iframe']
# ...
end
My app called Tapp, I'm pretty sure you will have another name here ;)
Then in the console:
[retgoat#iMac-Roman ~/workspace/tapp]$ rc
Loading development environment (Rails 4.2.6)
[1] pry(main)> Tapp::Application.config.action_view[:sanitized_allowed_tags]
=> ["strong", "em", "a", "br", "iframe"]

Ruby on Rails error when creating record

I am following the Kevin Skoglund tutorial for Ruby on Rails called Ruby on Rails 4 Essential Training. In the section 'Create Record' I am having the following error after simply trying to create a record:
George$ pwd
/Users/George/Sites/simple_cms
George$ rails console
Loading development environment (Rails 4.2.0)
irb(main):001:0> subject = Subject.new
NameError: uninitialized constant Subject
from (irb):1
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/George/Sites/simple_cms/bin/rails:8:in `<top (required)>'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
irb(main):002:0>
As I am still in the early stages of learning I cannot establish what the cause might be although it appears to be gem related based on the errors return.
If anyone has any suggestion and maybe a tip on how I can work out what the error and fix is for myself it would be appreciated as I really want to keep learning.
George$ pwd
/Users/George/Sites/simple_cms
George$ rails console
Loading development environment (Rails 4.2.0)
> rails generate model Subject name:string type:string
> rake db:migrate
> rails console
> sub = Subject.new
Try this.

uninitialized constant ActiveRecord

2.0.0-p247 :006 > load './app/models/user.rb'
NameError: uninitialized constant ActiveRecord
from /home/action/iAuth/app/models/user.rb:1:in `<top (required)>'
from (irb):6:in `load'
from (irb):6
from /home/action/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'
Below is my User Model.
class User < ActiveRecord::Base
end
When i try to load my user.rb in my irb, i get the above error.
start your irb session with
rails console
and not:
irb
rails console would load your rails environment and your model for you, so you can do things like:
User.all or User.new without loading the class as it has been preloaded by rails console already
there's another case.
if you are using mongoid ( mongo db adapter) , and you have to make sure in config/application.rb, you must require activerecord:
require "active_model/railtie"

rails 3.2 and machinist issues

I've just upgraded to Rails 3.2.1 with Ruby 1.9.3-p0 and I'm using Machinist 2.0. Before updating a large project all my tests passed. The problem I"m having is when I create a blueprint within a 'let' call in my rspec tests and then refer to it in a before do block.
let (:new_post) {Post.make!}
before do
Post.stub!(:new).and_return(new_post)
end
This used to work, and now I get the following error:
1) PostsController GET index assigns all posts as #posts
Failure/Error: let (:new_post) {Post.make!}
NoMethodError:
undefined method `title=' for nil:NilClass
# ./spec/support/blueprints.rb:22:in `block in <top (required)>'
# ./spec/controllers/posts_controller_spec.rb:37:in `block (2 levels) in <top (required)>'
# ./spec/controllers/posts_controller_spec.rb:40:in `block (2 levels) in <top (required)>'
Here is my blueprint:
require 'machinist/active_record'
Post.blueprint do
title {"Post"}
body {"hello world"}
end
For now my work around is to create them using instance variables within the before do block, but it would be nice to use the 'let' calls as it keeps my rspec tests cleaner.
Funny, I just ran across the exact same problem, although I'm on Rails 3.2.1, Machinist 2.0, and ruby 1.9.2-p290. I think there's a conflict between the execution of the Post.stub(:new) stub method and the Machinist make method, but I haven't dug into the code.
The best solution I've come up with is:
before do
new_post
Post.stub!(:new).and_return(new_post)
end
This will initialize the let (since let is lazy-loaded in rspec) before it gets to the stub method. It's hacky, but at least you (and I) can keep the let statement.

ACL9 and Rails 2.3.8 - NameError: uninitialized constant User::Role

I am running RVM (Ruby 1.8.7-head, Rails 2.3.8) and have the BASICS of a new app running. I have Authlogic working, with the minimal code to make that work. I am attempting to use ACL9 (which I have working on a different project, same RVM gemset)
class User < ActiveRecord::Base
# authentication
acts_as_authentic
# authorization
acts_as_authorization_subject
def full_name
"#{self.first_name} #{self.last_name}"
end
end
When I attempt to just run a simple check to make sure ACL9 is working properly...
script/console
u = User.first
u.has_role?(:anyrole)
I get this error...
ruby-1.8.7-head > u.has_role?(:anyrole)
NameError: uninitialized constant User::Role
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in `const_missing'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/base.rb:1:in `compute_type'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activesupport-2.3.8/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/base.rb:2230:in `compute_type'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/reflection.rb:156:in `send'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/reflection.rb:156:in `klass'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/reflection.rb:187:in `quoted_table_name'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations/has_and_belongs_to_many_association.rb:102:in `construct_sql'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:21:in `initialize'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations/has_and_belongs_to_many_association.rb:5:in `initialize'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations.rb:1306:in `new'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations.rb:1306:in `role_objects'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/acl9-0.12.0/lib/acl9/model_extensions/for_subject.rb:39:in `has_role?'
from (irb):2
I'm not sure why this works with every other app I have, but not this one - and I'm not sure what the error message is saying.. any help is appreciated.
class Role < ActiveRecord::Base
acts_as_authorization_role
end

Resources