1

I am implementing griddler(using Mandrill) to fetch e-mail in my app.

This is my class to receive mail

#app/email_receivers/incoming.rb
 class EmailReceiver < Incoming::Strategies::Mandrill
   def receive(mail)
    %(Got message from #{mail.to.first} with subject "#{mail.subject}")
   end
  end

  req = Rack::Request.new(Rails.env)
  result = EmailReceiver.receive(req) 

This is my rails controller that calls receive method of EmailReceiver class.

#emails_controller.rb
 class EmailsController < ActionController::Base
   def create
    if EmailReceiver.receive(request)
     render :json => { :status => 'ok' }
    else
     render :json => { :status => 'rejected' }, :status => 403
    end
   end
 end

Routes for calling create method of EmailsController

#routes.rb
 Rails.application.routes.draw do
   post '/emails' => 'emails#create'
 end

Note:- After running server in production environment

rails s -e production

I got this error

/home/bvc-2/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/request.rb:192:in `[]=': string not matched (IndexError)

Where "puts req" in

req = Rack::Request.new(Rails.env) 

turns out to be:

#<Rack::Request:0xc3bace8 @env="production">

And my config/application.rb file is:-

require File.expand_path('../boot', __FILE__)

require 'rails/all'

 # Require the gems listed in Gemfile, including any gems
 # you've limited to :test, :development, or :production.
 Bundler.require(*Rails.groups)

 ActionMailer::Base.smtp_settings = {
:address =>"smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name =>  "***@gmail.com",
:password => "pass*******",
:authentication => 'plain',
:enable_starttls_auto => true
}
 module ComplaintSystem
 class Application < Rails::Application

  # Settings in config/environments/* take precedence over those specified  here.
  # Application configuration should go into files in config/initializers
  # -- all .rb files in that directory are automatically loaded.

  # Set Time.zone default to the specified zone and make Active Record    auto-convert to this zone.
  # Run "rake -D time" for a list of tasks for finding time zone names.    Default is UTC.
  # config.time_zone = 'Central Time (US & Canada)'
    config.active_job.queue_adapter = :delayed_job
    config.time_zone = 'Mumbai'
    config.active_record.default_timezone = :local
    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
      config.active_record.raise_in_transactional_callbacks = true
 end
end
vidal
  • 345
  • 3
  • 18

1 Answers1

1

This type of error happens when you try to access a string variable as a Hash by mistake.

s = "hello world"
s["position"] = "programming is fun"
# > IndexError: string not matched
# > from (irb):5:in `[]='
# > from (irb):5

Look at your full stack trace and see where you are trying to do such operation.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • `req = Rack::Request.new(Rails.env)` returns a `Rack::Request` object and you are sending that to the ` EmailReceiver.receive(...)` method. What do you really want to accomplish there? you are not even using this in your `receive` method. – K M Rakibul Islam Aug 26 '15 at 11:44
  • @K M Rakibul Islam Error is in `req = Rack::Request.new(Rails.env) result = EmailReceiver.receive(req)` The variable is **req ** – vidal Aug 26 '15 at 11:47
  • Actually I want to receive emails to my app on production environment.Are these two lines essential? `req = Rack::Request.new(Rails.env)` `result = EmailReceiver.receive(req)` I really don't know what are they doing..any help appreciated thanks – vidal Aug 26 '15 at 11:54
  • please show your `config/application.rb` file. How did you configure that? – K M Rakibul Islam Aug 26 '15 at 11:56
  • You should have some setting for your email in your `application.rb` which should take care of sending the emails in production. If thats the case, try after commenting these two lines: req = Rack::Request.new(Rails.env) result = EmailReceiver.receive(req) – K M Rakibul Islam Aug 26 '15 at 11:59
  • added config/application.rb file, go through it – vidal Aug 26 '15 at 12:19
  • try without those two lines with problems. – K M Rakibul Islam Aug 26 '15 at 12:20