I'm following a clickatell tutorial and my code looks as follows. However I get the error
uninitialized constant ActionDispatch::Routing::Routes (NameError)
from /Library/Ruby/Gems/1.8/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:258:in `instance_exec'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:258:in `eval_block'
from /Library/Ruby/Gems/1.8/gems/actionpack-3.1.1/lib/action_dispatch/routing/route_set.rb:235:in `draw'
from /Users/user1/Desktop/rails_projects/clickatellsms/config/routes.rb:1
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/routes_reloader.rb:29:in `load_paths'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/routes_reloader.rb:29:in `each'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/routes_reloader.rb:29:in `load_paths'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/routes_reloader.rb:13:in `reload!'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/routes_reloader.rb:7:in `initialize'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.1/lib/active_support/file_update_checker.rb:32:in `call'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.1/lib/active_support/file_update_checker.rb:32:in `execute_if_updated'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/finisher.rb:63
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/finisher.rb:64:in `call'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application/finisher.rb:64
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/initializable.rb:30:in `instance_exec'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/initializable.rb:30:in `run'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/initializable.rb:55:in `run_initializers'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/initializable.rb:54:in `each'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/initializable.rb:54:in `run_initializers'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/application.rb:96:in `initialize!'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/railtie/configurable.rb:30:in `send'
from /Library/Ruby/Gems/1.8/gems/railties-3.1.1/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/user1/Desktop/rails_projects/clickatellsms/config/environment.rb:5
from /Users/user1/Desktop/rails_projects/clickatellsms/config.ru:4:in `require'
from /Users/user1/Desktop/rails_projects/clickatellsms/config.ru:4
from /Library/Ruby/Gems/1.8/gems/rack-1.3.4/lib/rack/builder.rb:51:in `instance_eval'
from /Library/Ruby/Gems/1.8/gems/rack-1.3.4/lib/rack/builder.rb:51:in `initialize'
from /Users/user1/Desktop/rails_projects/clickatellsms/config.ru:1:in `new'
from /Users/user1/Desktop/rails_projects/clickatellsms/config.ru:1
My code looks like this -
# controllers/sms.rb:
require 'clickatell'
class SMS
def initialize(config)
#config = config
end
def create(recipient, message_text)
api.send_message(recipient, message_text)
end
private
def api
#api ||= Clickatell::API.authenticate(
#config[:api_key],
#config[:username],
#config[:password])
end
end
# config/clickatell.yml:
api_key: 9999999
username: abcdefg
password: hijklmno
# config/environments/development.rb
CLICKATELL_CONFIG = YAML.load(File.open(File.join(RAILS_ROOT,
'config', 'clickatell.yml')))
# config/routes.rb:
ActionController::Routing::Routes.draw do |map|
map.resource :sms
end
# app/views/sms/new.html.erb:
<% form_tag '/sms', :method => :post do -%>
<label>Enter the recipients mobile number:</label>
<%= text_field_tag "recipient" %>
<label>Enter your message:</label>
<%= text_area_tag "message_text" %>
<%= submit_tag "Send SMS" %>
<% end %>
# controllers/sms_controller.rb:
class SmsController < ApplicationController
def create
sms = SMS.new(CLICKATELL_CONFIG)
sms.create(params[:recipient], params[:message_text])
flash[:notice] = "Message sent succesfully!"
redirect_to :back
rescue Clickatell::API::Error => e
flash[:error] = "Clickatell API error: #{e.message}"
redirect_to :back
end
end
That's your routes.rb which is non sense:
ActionController::Routing::Routes.draw do |map|
map.resource :sms
end
It should rather be:
APP_NAME_HERE_CAMEL_CASE_STYLE::Application.routes.draw do
resource :sms
end
Looks like you copy/pasted old Rails 2 code within a Rails 3 app.
Related
I recently ran into an issue with an integration test using rspec.
RSpec.describe "Images", type: :request do
before(:each) do
#user = User.create(username: "sam", password: "password")
#tag = #user.tags.create(name: "outdoors")
post login_path, params: { username: "sam", password: "password" }
expect(session[:user_id]).to eql #user.id
#file = fixture_file_upload("/dingo.jpeg", "image/jpeg")
end
it "can show images" do
#image = #user.images.create(caption: "hello", image_file: #image)
get user_image_path(#user, #image)
expect(response).to be_success
end
end
The before each creates a valid user, tag and logs the user in. Then it loads an image file.
The test it's self creates a new image for the #user and assigns it a value for caption and the loaded file for its :image_file.
The Image model uses has_one_attached :image_file to associate an uploaded image with an image record
class Image < ApplicationRecord
belongs_to :user
has_and_belongs_to_many :tags
has_one_attached :image_file
end
the controller and action as follows:
class ImageController < ApplicationController
before_action :unauthorized_redirect
def show
#user = current_user
#image = Image.find(params[:id])
#tags = #user.tags.all
authorize #image, :show?
end
private
def image_params
params.require(:image).permit(:caption, :image_file, tag_ids: [])
end
end
When I run the test I get an error:
Failures:
1) Images can show images
Failure/Error: <%= image_tag #image.image_file, class: "img-fluid show-image" %>
ActionView::Template::Error:
Can't resolve image into URL: undefined method `persisted?' for nil:NilClass
# ./app/views/image/show.html.erb:7:in `_app_views_image_show_html_erb__3727703739275822003_20580'
# <internal:kernel>:90:in `tap'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/actiontext-6.1.4.1/lib/action_text/rendering.rb:20:in `with_renderer'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/actiontext-6.1.4.1/lib/action_text/engine.rb:59:in `block (4 levels) in <class:Engine>'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/tempfile_reaper.rb:15:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/etag.rb:27:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/conditional_get.rb:27:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/head.rb:12:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:266:in `context'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:260:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:37:in `call_app'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:26:in `block in call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:26:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/runtime.rb:22:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/engine.rb:539:in `call'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:119:in `request'
# /home/sam/.rvm/gems/ruby-3.0.0/gems/rails-controller-testing-1.0.5/lib/rails/controller/testing/integration.rb:16:in `block (2 levels) in <module:Integration>'
# ./spec/requests/images_spec.rb:38:in `block (2 levels) in <main>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `persisted?' for nil:NilClass
# ./app/views/image/show.html.erb:7:in `_app_views_image_show_html_erb__3727703739275822003_20580'
Finished in 0.79372 seconds (files took 0.46552 seconds to load)
30 examples, 1 failure
Failed examples:
rspec ./spec/requests/images_spec.rb:36 # Images can show images
I have debugged this issue with breakpoints everywhere and I just don't understand why it's not working in test when it works in production. I hope I've provided enough information this bug has been driving me crazy for the past two days.
Thank you!
Versions:
rails: 6.1.4.1
rspec: 5.0.2
Changed one line and fixed it
it "can show images" do
#image = #user.images.create(caption: "hello", image_file: #image)
get user_image_path(#user, #image)
*expect(response).to be_success ----REMOVE*
expect(response.successful?).to eql true
end
it "renders index page" do
projects_user = FactoryGirl.create(:projects_user)
get :index, id: projects_user.id
expect(response).to render_template :index
end
this is my controller test, when i run it it says: Validation failed: Countries can't be blank
I am new to stackoverflow and ruby soo any help i get is great
FilesController:
class FilesController < ApplicationController
before_filter :authenticate_user!
def index
#pu = ProjectsUser.find(params[:id])
debugger
#files = UserFile.includes(:user).where(project_id: #pu.id)
end
def new
#file = UserFile.new
end
def create
#file = UserFile.new(file_params)
if #file.save
flash[:success] = "File successfully uploaded!"
redirect_to files_path
else
#files = UserFile.includes(:user).where(user_id: current_user.id)
render :index
end
end
def destroy
#file = UserFile.find(params[:file_id])
if #file.destroy
flash[:success] = "File successfully deleted!"
redirect_to files_path
else
flash[:error] = "File could not be deleted."
redirect_to files_path
end
end
protected
def file_params
params.require(:user_file).permit(:file, :user_id, :created_at, :updated_at, :project_id)
end
end
Country controller:
class CountriesController < ApplicationController
def states
#states = State.where(country_id: params[:id]).all
return render :json => {success: true, status: 200, data: #states}
end
end
Country model:
class Country < ActiveRecord::Base
validates :name, presence: true
has_many :states
has_many :users
has_and_belongs_to_many :projects
validates :name, presence: true
end
Factory for country:
require 'ffaker'
FactoryGirl.define do
factory :country do
name {FFaker::AddressUS.country}
end
end
output of error:
ActiveRecord::RecordInvalid:
Validation failed: Countries can't be blank
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib /factory_girl/configuration.rb:18:in `block in initialize'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluation.rb:15:in `[]'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluation.rb:15:in `create'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:12:in `block in result'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:9:in `tap'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:9:in `result'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory.rb:42:in `run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory_runner.rb:28:in `run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:5:in `association'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluator.rb:33:in `association'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute/association.rb:19:in `block in to_proc'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluator.rb:77:in `instance_exec'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluator.rb:77:in `block in define_attribute'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:56:in `get'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/evaluation.rb:12:in `object'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy/create.rb:9:in `result'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory.rb:42:in `run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/factory_runner.rb:28:in `run'
# /home/nemanja/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.8.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
# ./spec/controllers/files_controllers_spec.rb:31:in `block (3 levels) in <top (required)>'
UPDATE:
Factory for project, project has_and_belongs_to_many countries
require 'ffaker'
FactoryGirl.define do
factory :project do
sponsor_name "John Pare"
title "Anea"
start_date Date.new(2016, 5, 7)
end_date Date.new(2017, 3, 6)
description {FFaker::Lorem.paragraph}
number_of_sites_planned "5"
estimated_enrollment "24"
feasibility_questionnaire File.open("#{Rails.root}/spec/internal_spec_files/app.pdf")
association :disease_type
association :disease_condition
association :user
association :project_category
association :project_subcategory
after(:create) do |m|
m.countries << build(:country)
end
end
end
The problem is within the
projects_user = FactoryGirl.create(:projects_user)
the creation code (where you defined this factory) does not set a country and this is why you get an error.
I'm new to Ruby on Rails and am learning to use Angular with it, but after I ran "gem install rack-cors", when I try to start a Rails application, I keep getting this error:
C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:35:in `build': undefined method `new' for "Rack::Cors":String (NoMethodError)
Did you mean? next
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:99:in `block in build'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:99:in `each'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:99:in `inject'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:99:in `build'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/engine.rb:508:in `block in app'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/engine.rb:504:in `synchronize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/engine.rb:504:in `app'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/application/finisher.rb:45:in `block in <module:Finisher>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/initializable.rb:30:in `instance_exec'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/initializable.rb:30:in `run'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/initializable.rb:59:in `block in run_initializers'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in `each'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in `call'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/initializable.rb:58:in `run_initializers'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/application.rb:353:in `initialize!'
from D:/TFE Unipar/Vigia Back-End/config/environment.rb:5:in `<top (required)>'
from config.ru:3:in `require_relative'
from config.ru:3:in `block in <main>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/builder.rb:55:in `instance_eval'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/builder.rb:55:in `initialize'
from config.ru:in `new'
from config.ru:in `<main>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/builder.rb:49:in `eval'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/builder.rb:49:in `new_from_string'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/builder.rb:40:in `parse_file'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/server.rb:319:in `build_app_and_options_from_config'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/server.rb:219:in `app'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:24:in `app'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-2.0.2/lib/rack/server.rb:354:in `wrapped_app'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:80:in `log_to_stdout'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:42:in `start'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:131:in `block in perform'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:126:in `tap'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands/server/server_command.rb:126:in `perform'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/command/base.rb:63:in `perform'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/command.rb:44:in `invoke'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-5.1.1/lib/rails/commands.rb:16:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>
This is the file that's mentioned on the error:
require "active_support/inflector/methods"
require "active_support/dependencies"
module ActionDispatch
class MiddlewareStack
class Middleware
attr_reader :args, :block, :klass
def initialize(klass, args, block)
#klass = klass
#args = args
#block = block
end
def name; klass.name; end
def ==(middleware)
case middleware
when Middleware
klass == middleware.klass
when Class
klass == middleware
end
end
def inspect
if klass.is_a?(Class)
klass.to_s
else
klass.class.to_s
end
end
def build(app)
klass.new(app, *args, &block)
end
end
include Enumerable
attr_accessor :middlewares
def initialize(*args)
#middlewares = []
yield(self) if block_given?
end
def each
#middlewares.each { |x| yield x }
end
def size
middlewares.size
end
def last
middlewares.last
end
def [](i)
middlewares[i]
end
def unshift(klass, *args, &block)
middlewares.unshift(build_middleware(klass, args, block))
end
def initialize_copy(other)
self.middlewares = other.middlewares.dup
end
def insert(index, klass, *args, &block)
index = assert_index(index, :before)
middlewares.insert(index, build_middleware(klass, args, block))
end
alias_method :insert_before, :insert
def insert_after(index, *args, &block)
index = assert_index(index, :after)
insert(index + 1, *args, &block)
end
def swap(target, *args, &block)
index = assert_index(target, :before)
insert(index, *args, &block)
middlewares.delete_at(index + 1)
end
def delete(target)
middlewares.delete_if { |m| m.klass == target }
end
def use(klass, *args, &block)
middlewares.push(build_middleware(klass, args, block))
end
def build(app = Proc.new)
middlewares.freeze.reverse.inject(app) { |a, e| e.build(a) }
end
private
def assert_index(index, where)
i = index.is_a?(Integer) ? index : middlewares.index { |m| m.klass == index }
raise "No such middleware to insert #{where}: #{index.inspect}" unless i
i
end
def build_middleware(klass, args, block)
Middleware.new(klass, args, block)
end
end
end
How do I solve it?
When using Rails 5 or Rails 6, remove the quotes.
module YourApp
class Application < Rails::Application
# ...
# Rails 3/4
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
# Rails 5/6
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
end
end
See https://github.com/cyu/rack-cors#rails-configuration
I have almost completed this tutorial when I started to get these error messages. Can someone give me a starting point on where to begin find out whats wrong. What files do you all need to see?
Failures:
1) MicropostsController DELETE 'destroy' for an unauthorized user should deny access
Failure/Error: response.should redirect_to(root_path)
Expected response to be a redirect to <http://test.host/> but was a redirect to <http://test.host/signin>
# ./spec/controllers/microposts_controller_spec.rb:72:in `block (4 levels) in <top (required)>'
2) UsersController GET 'new' should be successful
Failure/Error: get :new
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000005a62f70>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100'
# ./spec/controllers/users_controller_spec.rb:131:in `block (3 levels) in <top (required)>'
3) UsersController GET 'new' should have the right title
Failure/Error: get :new
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000004242df0>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100'
# ./spec/controllers/users_controller_spec.rb:136:in `block (3 levels) in <top (required)>'
4) UsersController GET 'new' POST 'create' failure should have the right title
Failure/Error: post :create, :user => #attr
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000004c96518>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/controllers/users_controller.rb:29:in `create'
# ./spec/controllers/users_controller_spec.rb:148:in `block (5 levels) in <top (required)>'
5) UsersController GET 'new' POST 'create' failure should render the 'new' page
Failure/Error: post :create, :user => #attr
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000056482c8>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/controllers/users_controller.rb:29:in `create'
# ./spec/controllers/users_controller_spec.rb:152:in `block (5 levels) in <top (required)>'
6) UsersController GET 'new' POST 'create' failure should not create a user
Failure/Error: post :create, :user => #attr
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000051f8038>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/controllers/users_controller.rb:29:in `create'
# ./spec/controllers/users_controller_spec.rb:157:in `block (6 levels) in <top (required)>'
# ./spec/controllers/users_controller_spec.rb:156:in `block (5 levels) in <top (required)>'
7) UsersController GET 'new' GET 'edit' should be succesful
Failure/Error: get :edit, :id => #user
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000053301f8>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./spec/controllers/users_controller_spec.rb:197:in `block (4 levels) in <top (required)>'
8) UsersController GET 'new' GET 'edit' should have the right title
Failure/Error: get :edit, :id => #user
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x0000000508af20>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./spec/controllers/users_controller_spec.rb:202:in `block (4 levels) in <top (required)>'
9) UsersController GET 'new' GET 'edit' should have a link to change the gravatar
Failure/Error: get :edit, :id => #user
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000059d7d08>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./spec/controllers/users_controller_spec.rb:207:in `block (4 levels) in <top (required)>'
10) UsersController GET 'new' PUT 'update' should change the user's attributes
Failure/Error: put :update, :id => #user, :user => #attr
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000004cf6990>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./app/controllers/users_controller.rb:43:in `update'
# ./spec/controllers/users_controller_spec.rb:247:in `block (4 levels) in <top (required)>'
11) UsersController GET 'new' PUT 'update' should have the right title
Failure/Error: put :update, :id => #user, :user => #attr
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000054200b8>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./app/controllers/users_controller.rb:43:in `update'
# ./spec/controllers/users_controller_spec.rb:255:in `block (4 levels) in <top (required)>'
12) UsersController GET 'new' PUT 'update' failure should render the 'edit' page
Failure/Error: put :update, :id => #user, :user => #attr
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000057c0448>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./app/controllers/users_controller.rb:43:in `update'
# ./spec/controllers/users_controller_spec.rb:231:in `block (5 levels) in <top (required)>'
13) FriendlyForwardings should forward to the requested page after signin
Failure/Error: click_button
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x0000000541df48>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/edit.html.erb:5:in `block in _app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb___4049085790763719618_40974080'
# ./spec/requests/friendly_forwardings_spec.rb:10:in `block (2 levels) in <top (required)>'
14) LayoutLinks should have an signup page at '/signup'
Failure/Error: get '/signup'
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000003980370>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100'
# ./spec/requests/layout_links_spec.rb:28:in `block (2 levels) in <top (required)>'
15) LayoutLinks should have the right links on the layout
Failure/Error: click_link "Sign up now!"
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000058c3098>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100'
# ./spec/requests/layout_links_spec.rb:47:in `block (2 levels) in <top (required)>'
16) creation failure should not make a new micropost
Failure/Error: fill_in :micropost_content, :with => ""
Webrat::NotFoundError:
Could not find field: :micropost_content
# ./spec/requests/microposts_spec.rb:17:in `block (4 levels) in <top (required)>'
# ./spec/requests/microposts_spec.rb:15:in `block (3 levels) in <top (required)>'
17) creation success should make a new micropost
Failure/Error: fill_in :micropost_content, :with => content
Webrat::NotFoundError:
Could not find field: :micropost_content
# ./spec/requests/microposts_spec.rb:30:in `block (4 levels) in <top (required)>'
# ./spec/requests/microposts_spec.rb:28:in `block (3 levels) in <top (required)>'
18) Users signup failure should not make a new user
Failure/Error: visit signup_path
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x00000005766c40>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100'
# ./spec/requests/users_spec.rb:11:in `block (5 levels) in <top (required)>'
# ./spec/requests/users_spec.rb:10:in `block (4 levels) in <top (required)>'
19) Users signup success should make a new
Failure/Error: visit signup_path
ActionView::Template::Error:
undefined local variable or method `object' for #<#<Class:0x000000051d2680>:0x000000059e8bd0>
# ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb___1348716384004617740_45632520'
# ./app/views/users/_fields.html.erb:2:in `_app_views_users__fields_html_erb___3244409800155025389_45601120'
# ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb___2634588673674572576_47386100'
# ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___2634588673674572576_47386100'
# ./spec/requests/users_spec.rb:27:in `block (5 levels) in <top (required)>'
# ./spec/requests/users_spec.rb:26:in `block (4 levels) in <top (required)>'
Finished in 4.31 seconds
124 examples, 19 failures
Failed examples:
rspec ./spec/controllers/microposts_controller_spec.rb:70 # MicropostsController DELETE 'destroy' for an unauthorized user should deny access
rspec ./spec/controllers/users_controller_spec.rb:130 # UsersController GET 'new' should be successful
rspec ./spec/controllers/users_controller_spec.rb:135 # UsersController GET 'new' should have the right title
rspec ./spec/controllers/users_controller_spec.rb:147 # UsersController GET 'new' POST 'create' failure should have the right title
rspec ./spec/controllers/users_controller_spec.rb:151 # UsersController GET 'new' POST 'create' failure should render the 'new' page
rspec ./spec/controllers/users_controller_spec.rb:155 # UsersController GET 'new' POST 'create' failure should not create a user
rspec ./spec/controllers/users_controller_spec.rb:196 # UsersController GET 'new' GET 'edit' should be succesful
rspec ./spec/controllers/users_controller_spec.rb:201 # UsersController GET 'new' GET 'edit' should have the right title
rspec ./spec/controllers/users_controller_spec.rb:206 # UsersController GET 'new' GET 'edit' should have a link to change the gravatar
rspec ./spec/controllers/users_controller_spec.rb:246 # UsersController GET 'new' PUT 'update' should change the user's attributes
rspec ./spec/controllers/users_controller_spec.rb:254 # UsersController GET 'new' PUT 'update' should have the right title
rspec ./spec/controllers/users_controller_spec.rb:230 # UsersController GET 'new' PUT 'update' failure should render the 'edit' page
rspec ./spec/requests/friendly_forwardings_spec.rb:4 # FriendlyForwardings should forward to the requested page after signin
rspec ./spec/requests/layout_links_spec.rb:27 # LayoutLinks should have an signup page at '/signup'
rspec ./spec/requests/layout_links_spec.rb:38 # LayoutLinks should have the right links on the layout
rspec ./spec/requests/microposts_spec.rb:14 # creation failure should not make a new micropost
rspec ./spec/requests/microposts_spec.rb:26 # creation success should make a new micropost
rspec ./spec/requests/users_spec.rb:8 # Users signup failure should not make a new user
rspec ./spec/requests/users_spec.rb:25 # Users signup success should make a new
# Finished at 2012-05-15 16:41:20
This is _error_messages.html.erb
<% if object.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(object.errors.count, "error") %>
prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved:
</h2>
<p>There were problems with the following fields:</p>
<ul>
<% object.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
This my _micropost_form.html.erb
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.text_area :content %>
</div>
<div class="=actions">
<%= f.submit "Submit" %>
</div>
<%end%>
This is _fields.html.erb
<%= render 'shared/error_messages'%>
<div class="field">
<%= f.label :name %><br/>
<%= f.text_field :name%>
</div>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br/>
<%= f.password_field :password_confirmation %>
</div>
Errors 2 through 15 have a simple fix: your _fields.html.erb is missing some code on the first line. The entire line should read:
app/views/layouts/_fields.html.erb
<%= render 'shared/error_messages', object: f.object %> #object: f.object allows the form variable to
#access the associated object.
Unfortunately I can't do anything with the rest of your errors since your relevant code isn't attached.
This is still an issue in the latest version of the book as of this posting.
Earlier in the book the fields for user related forms were refactored and put in a partial _fields.html.erb where the shared/error_messages was rendered.
The users/new and users/edit files don't need to be modified, as stated in the book, and instead the only part that needs to be modified is _fields.html.erb to pass object: f.object to shared/error_messages
I had this issue as well, to fix it I didn't adjust the code from Michael's example, I just moved the _fields.html.erb partial from the views/users folder to the views/shared folder. Once in the shared folder it seemed to accept objects from both controllers. Hope this helps.
I think you need to go to the file that is calling the _error_messages partial and simply make sure you're calling it using the following:
<%= render 'shared/error_messages', object: f.object %>
This is because you're adding the hidden field tag.
I'm trying to send mail in a Rails 3 application using collectiveidea's delayed_job. If I try to send mail regularly (.deliver) it works fine, but as soon as I switch to delayed job, things fall to pieces.
The standard error I get in the delayed_job mysql table is:
{undefined method `name' for
nil:NilClass...
where 'name' is the first argument in the mailer's view (#contact.name). This works fine if I take delayed_job out again.
If I remove all references to #contact in the view, I get
{A sender (Return-Path, Sender or
From) required to send a message
In short, it doesn't seem to be understanding any arguments at all.
All relevant code below - if anyone has any suggestions for this it would be very appreciated
CONTROLLER
def sendmail
#contact = Contact.new(params[:contact])
if #contact.save
ContactMailer.delay.contact_mail(#contact)
flash[:notice] = 'Your message has been successfully sent'
redirect_to root_path
else
render :action => 'index'
end
end
MAILER:
class ContactMailer < ActionMailer::Base
default :from => "my#email.address"
def contact_mail(contact)
#contact = contact
mail(:to => 'my#email.address', :subject => 'Contact Form Query', :from => 'my#email.address', :content_type => 'text/plain')
end
handle_asynchronously :contact_mail, :run_at => Proc.new { 2.seconds.from_now }
end
MAILER VIEW:
You have received a new query:
-----------------------------------
<%= #contact.name %>
<%= #contact.business %>
<%= #contact.phone %>
-----------------------
<%= #contact.message %>
-----------------------
INITIALIZER:
Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.max_run_time = 5.weeks
And finally, the full error message:
{undefined method `name' for nil:NilClass
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/john/Websites/Rails/InDevelopment/connectted/app/views/contact_mailer/contact_mail.html.erb:4:in `_app_views_contact_mailer_contact_mail_html_erb___3386534441642202773_2166008980__4301703149293725172'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/template.rb:135:in `block in render'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications.rb:54:in `instrument'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/template.rb:127:in `render'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:59:in `block in _render_template'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications.rb:52:in `block in instrument'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications.rb:52:in `instrument'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:56:in `_render_template'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:26:in `render'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:114:in `_render_template'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:108:in `render_to_body'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:101:in `render_to_string'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:92:in `render'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/deprecated_api.rb:111:in `render'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:210:in `block in create_parts'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:208:in `each'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:208:in `create_parts'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/deprecated_api.rb:143:in `create_parts'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:77:in `process'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:446:in `process'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:441:in `initialize'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:425:in `new'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:425:in `method_missing'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/performable_mailer.rb:6:in `perform'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/backend/base.rb:83:in `invoke_job'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:119:in `block (2 levels) in run'
/Users/john/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/timeout.rb:57:in `timeout'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed
/worker.rb:119:in `block in run'
/Users/john/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:118:in `run'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:176:in `reserve_and_run_one_job'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:103:in `block in work_off'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:102:in `times'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:102:in `work_off'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:77:in `block (2 levels) in start'
/Users/john/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:76:in `block in start'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:73:in `loop'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:73:in `start'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:100:in `run'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:79:in `block in run_process'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:250:in `call'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:250:in `block in start_proc'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/daemonize.rb:199:in `call'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/daemonize.rb:199:in `call_as_daemon'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:254:in `start_proc'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:294:in `start'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/controller.rb:70:in `run'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons.rb:193:in `block in run_proc'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `call'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `catch_exceptions'
/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons.rb:192:in `run_proc'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:78:in `run_process'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:72:in `block in daemonize'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:70:in `times'
/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:70:in `daemonize'
script/delayed_job:5:in `<main>' |
The issue is with the code in your mailer taking the object instead of the objects id:
def contact_mail(contact)
#contact = contact
mail(:to => 'my#email.address', :subject => 'Contact Form Query', :from => 'my#email.address', :content_type => 'text/plain')
end
You should change that to this:
def contact_mail(contact_id)
#contact = Contact.find(contact_id)
mail(:to => 'my#email.address', :subject => 'Contact Form Query', :from => 'my#email.address', :content_type => 'text/plain')
end
Never pass the actual objects to your mailer. Simply pass the id's and retrieve them upon processing.
I had this same problem, and fixed it by moving all the attributes into instance variables. So, in the view:
<%= #contact.name %>
<%= #contact.business %>
<%= #contact.phone %>
becomes:
<%= #name %>
<%= #business %>
<%= #phone %>
and I just looked up the instance variables in the mailer method. So, in the mailer:
def contact_mail(contact)
#contact = contact
mail(:to => 'my#email.address', :subject => 'Contact Form Query', :from => 'my#email.address', :content_type => 'text/plain')
end
becomes:
def contact_mail(contact)
#name = contact.name
#business = contact.business
#phone = contact.phone
mail(:to => 'my#email.address', :subject => 'Contact Form Query', :from => 'my#email.address', :content_type => 'text/plain')
end
Why is it not working? Not sure, I imagine it has to do with some ActionMailer voodoo.
How to get it working? Abstract out the delayed job pieces into another class. Have the method that is delayed do nothing more than send them email; meaning that the rendering of the email is done in real time, but the sending of it is delayed. This should get around your problem and accomplish the asynchronous sending of emails.
Hey this might be far off, but you're not sending any params to your MAILER. Here's a copy of my referred_email.rb I keep in my lib/ dir. I call it with delayed_job:
#my controller
Delayed::Job.enqueue ReferredEmail.new(params[:subject], params[:editor1])
#Reffered_Email.rb
class ReferredEmail < Struct.new(:subject, :editor1)
def perform
(CardReferral.all.map(&:email) - CardSignup.all.map(&:email)).each do |cf|
#cf = CardReferral.find_by_email(cf)
Notifier.deliver_referred_magic_email(User.find(#cf.user_id), #cf.email, #cf.name, #cf.message, subject, editor1)
end
end
end
It seems like you are using both:
ContactMailer.delay.contact_mail(#contact)
and
handle_asynchronously :contact_mail, :run_at => Proc.new { 2.seconds.from_now }
which you should only do one or the other. Try removing the handle_asynchronously and see if that fixes the problem.