undefined method `encoding_aware?' - ruby

How do I resolve this issue in rails 4. I upgraded my project from rails 3 to rails 4.
My server console is throwing me below error message. Could not figure out.
undefined method `encoding_aware?' for #<String:0x000000053e6ca0>
Code :
module ActionView
class Template
module Handlers
class ERB
def call(template)
if template.source.encoding_aware? # here is the error
Error Log:
NoMethodError - undefined method `encoding_aware?' for #<String:0x00000004b05348>:
config/initializers/10-patches.rb:63:in `call'
actionview (4.2.0) lib/action_view/template.rb:270:in `compile'
actionview (4.2.0) lib/action_view/template.rb:245:in `block (2 levels) in compile!'
activesupport (4.2.0) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:244:in `block in compile!'
<internal:prelude>:10:in `synchronize'
actionview (4.2.0) lib/action_view/template.rb:232:in `compile!'
actionview (4.2.0) lib/action_view/template.rb:144:in `block in render'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:143:in `render'
rack-mini-profiler (0.9.1) lib/mini_profiler/profiling_methods.rb:108:in `block in profile_method'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.0) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.0) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.0) lib/action_view/rendering.rb:83:in `render_to_body'
my config/initializers/10-patchers.rb file
require 'active_record'
module ActiveRecord
class Base
include Redmine::I18n
# Translate attribute names for validation errors display
def self.human_attribute_name(attr, *args)
attr = attr.to_s.sub(/_id$/, '')
l("field_#{name.underscore.gsub('/', '_')}_#{attr}", :default => ["field_#{attr}".to_sym, attr])
end
end
# Undefines private Kernel#open method to allow using `open` scopes in models.
# See Defect #11545 (http://www.redmine.org/issues/11545) for details.
class Base
class << self
undef open
end
end
class Relation ; undef open ; end
end
module ActionView
module Helpers
module DateHelper
# distance_of_time_in_words breaks when difference is greater than 30 years
def distance_of_date_in_words(from_date, to_date = 0, options = {})
from_date = from_date.to_date if from_date.respond_to?(:to_date)
to_date = to_date.to_date if to_date.respond_to?(:to_date)
distance_in_days = (to_date - from_date).abs
I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
case distance_in_days
when 0..60 then locale.t :x_days, :count => distance_in_days.round
when 61..720 then locale.t :about_x_months, :count => (distance_in_days / 30).round
else locale.t :over_x_years, :count => (distance_in_days / 365).floor
end
end
end
end
end
class Resolver
def find_all(name, prefix=nil, partial=false, details={}, key=nil, locals=[])
cached(key, [name, prefix, partial], details, locals) do
if details[:formats] & [:xml, :json]
details = details.dup
details[:formats] = details[:formats].dup + [:api]
end
find_templates(name, prefix, partial, details)
end
end
end
end
# Do not HTML escape text templates
module ActionView
class Template
module Handlers
class ERB
def call(template)
if template.source.encoding_aware? # HERE IS THE ERROR LINE
# template.source.encoding_aware?
# encoding_aware? method has been deprecated or moved
# First, convert to BINARY, so in case the encoding is
# wrong, we can still find an encoding tag
# (<%# encoding %>) inside the String using a regular
# expression
template_source = template.source.dup.force_encoding("BINARY")
erb = template_source.gsub(ENCODING_TAG, '')
encoding = $2
erb.force_encoding valid_encoding(template.source.dup, encoding)
# Always make sure we return a String in the default_internal
erb.encode!
else
erb = template.source.dup
end
self.class.erb_implementation.new(
erb,
:trim => (self.class.erb_trim_mode == "-"),
:escape => template.identifier =~ /\.text/ # only escape HTML templates
).src
end
end
end
end
end
ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| html_tag || ''.html_safe }
require 'mail'
module DeliveryMethods
class AsyncSMTP < ::Mail::SMTP
def deliver!(*args)
Thread.start do
super *args
end
end
end
class AsyncSendmail < ::Mail::Sendmail
def deliver!(*args)
Thread.start do
super *args
end
end
end
class TmpFile
def initialize(*args); end
def deliver!(mail)
dest_dir = File.join(Rails.root, 'tmp', 'emails')
Dir.mkdir(dest_dir) unless File.directory?(dest_dir)
File.open(File.join(dest_dir, mail.message_id.gsub(/[<>]/, '') + '.eml'), 'wb') {|f| f.write(mail.encoded) }
end
end
end
ActionMailer::Base.add_delivery_method :async_smtp, DeliveryMethods::AsyncSMTP
ActionMailer::Base.add_delivery_method :async_sendmail, DeliveryMethods::AsyncSendmail
ActionMailer::Base.add_delivery_method :tmp_file, DeliveryMethods::TmpFile
# Changes how sent emails are logged
# Rails doesn't log cc and bcc which is misleading when using bcc only (#12090)
module ActionMailer
class LogSubscriber < ActiveSupport::LogSubscriber
def deliver(event)
recipients = [:to, :cc, :bcc].inject("") do |s, header|
r = Array.wrap(event.payload[header])
if r.any?
s << "\n #{header}: #{r.join(', ')}"
end
s
end
info("\nSent email \"#{event.payload[:subject]}\" (%1.fms)#{recipients}" % event.duration)
debug(event.payload[:mail])
end
end
end
module ActionController
module MimeResponds
class Collector
def api(&block)
any(:xml, :json, &block)
end
end
end
end
module ActionController
class Base
# Displays an explicit message instead of a NoMethodError exception
# when trying to start Redmine with an old session_store.rb
# TODO: remove it in a later version
def self.session=(*args)
$stderr.puts "Please remove config/initializers/session_store.rb and run `rake generate_secret_token`.\n" +
"Setting the session secret with ActionController.session= is no longer supported in Rails 3."
exit 1
end
end
end

encoding_aware? is deprecated in Rails 4.
If you take a quick look at the Rails 3 source for the String class you can find the implementation:
def encoding_aware?
true
end
So to fix your issue you can simply define the missing method by extending the String class, or find and replace any instances of calls to encoding_aware? with true.
This is how the extension would look:
class String
def encoding_aware?
true
end
end

Related

ruby not load font-awesome

i am trying to load font-awesome to my view
I have read other answers and implement the Rails: Using Font Awesome
What i have done
config/application.rb
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(:default, Rails.env)
module Blog
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)'
# 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
config.assets.paths << Rails.root.join("app", "assets", "fonts")
end
end
I have rename the font-awesome.min.css to font-awesome.min.css.erb and use assets_path
#font-face{font-family:'FontAwesome';
src:url('<%= asset_path("fontawesome-webfont.eot?v=4.0.3") %>');
src:url('<%= asset_path("awesome-webfont.eot?#iefix&v=4.0.3") %>') format('embedded-opentype'),
url('<%= asset_path("fontawesome-webfont.woff?v=4.0.3") %>') format('woff'),
url('<%= asset_path("fontawesome-webfont.ttf?v=4.0.3") %>') format('truetype'),
url('<%= asset_path("fontawesome-webfont.svg?v=4.0.3#fontawesomeregula") %>') format('svg');
font-weight:normal;font-style:normal}
and load the css file on the view
<%= stylesheet_link_tag 'bootstrap.css' %>
<%= stylesheet_link_tag 'animate.css' %>
<%= stylesheet_link_tag 'font-awesome.min.css' %>
<%= stylesheet_link_tag 'font.css' %>
<%= stylesheet_link_tag 'app.css' %>
and on Chrome the font files are not loaded
and on server i get
Started GET "/fontawesome-webfont.ttf?v=4.0.3" for 127.0.0.1 at 2014-02-14 08:36:37 +0200
ActionController::RoutingError (No route matches [GET] "/fontawesome-webfont.ttf"):
actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.2) lib/rails/engine.rb:511:in `call'
railties (4.0.2) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/usr/local/rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Please help
The easiest way to integrate font-awesome to rails is via font-awesome-rails gem. It will integrate font-awesome to rails asset pipeline.
In your Gemfile add
gem "font-awesome-rails"
and run bundle install to install the gem
Then in your scss file import it using
#import "font-awesome";
That's it! Now you can use it in your view as
<i class="fa fa-bell-o"></i>
If you prefer to use css instead of scss then add the require statement in your application.css
*= require font-awesome
Update
If you don't want to use font-awesome-rails gem then follow the steps
Place your fonts in the folder app/assets/fonts
In config/application.rb add the following lines
# Add fonts path
config.assets.paths << "#{Rails.root}/app/assets/fonts"
# Precompile additional assets
config.assets.precompile += %w( .svg .eot .woff .ttf )
Declare your font in font-awesome.css.erb like
#font-face {
font-family:'FontAwesome';
src: font-url('fontawesome-webfont.eot');
src: font-url('fontawesome-webfont?#iefix') format('embedded-opentype'),
font-url('fontawesome-webfont.woff') format('woff'),
font-url('fontawesome-webfont.ttf') format('truetype'),
font-url('fontawesome-webfont.svg') format('svg');
font-weight:normal;
font-style:normal;
}
I hope this will help you.

Subdomain sign in with Rails 4 and Devise 3.1.0

UPDATE --
I would recommend skipping direct to answer section -- goal of this was to allow multiple subdomains, each of which would have their own users/registrants. A registrant could register on multiple subdomains, however each subdomain is treated independently and unassociated with the others.
I feel this questions is still in need of a better answer so please contribute to it.
===============
ORIGINAL POST --
Help please --
scenario --
My site is used by :teams, each of which has :users. Im trying to enable :users to log in via the :team subdomain. (unique USER index keys are :email and :team_id -- the subdomain is part of a separate TEAM model that im trying to associate by :team_id in the user model during signin.)
TEAM MODEL
has_many :users
USER MODEL
devise request_keys: [:team_id]
belongs_to :team
def self.find_for_authentication(warden_conditions)
where(:email => warden_conditions[:email], :team_id => warden_conditions[:team_id]).first
end
this is per the new instruction on devise wiki: https://github.com/plataformatec/devise/wiki/How-to:-Scope-login-to-subdomain
APPLICATION CONTROLLER
I use before_action to set the team based on subdomain
I also add the strong params the "lazy" way for team_id / devise_parameter_sanitizer.for(:sign_in) << :team_id
DEVISE SESSIONS NEW VIEW (For USERS)
I feel dirty doing this, but I have a hidden team_id input field
AND THE ERROR DUMP NoMethodError undefined method team_id
Started POST "/users/sign_in" for 111.0.0.1 at 2013-09-13 13:08:09 -0400
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LONG TOKEN HERE=", "user"=>{"email"=>"user#site.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "team_id"=>"16", "commit"=>"Sign in"}
[1m[35mTeam Load (0.2ms)[0m SELECT "teams".* FROM "teams" WHERE "teams"."subdomain" = 'teamname' LIMIT 1
Completed 500 Internal Server Error in 119ms
NoMethodError (undefined method `team_id' for #<ActionDispatch::Request:0x007ffe950c2150>):
devise (3.1.0) lib/devise/strategies/authenticatable.rb:142:in `block in request_values'
devise (3.1.0) lib/devise/strategies/authenticatable.rb:142:in `map'
devise (3.1.0) lib/devise/strategies/authenticatable.rb:142:in `request_values'
devise (3.1.0) lib/devise/strategies/authenticatable.rb:122:in `with_authentication_hash'
devise (3.1.0) lib/devise/strategies/authenticatable.rb:71:in `valid_for_params_auth?'
devise (3.1.0) lib/devise/strategies/authenticatable.rb:16:in `valid?'
warden (1.2.3) lib/warden/proxy.rb:351:in `block in _run_strategies_for'
warden (1.2.3) lib/warden/proxy.rb:349:in `each'
warden (1.2.3) lib/warden/proxy.rb:349:in `_run_strategies_for'
warden (1.2.3) lib/warden/proxy.rb:319:in `_perform_authentication'
warden (1.2.3) lib/warden/proxy.rb:127:in `authenticate!'
devise (3.1.0) app/controllers/devise/sessions_controller.rb:15:in `create'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:463:in `_run__412684316733059520__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/mapper.rb:44:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1066722413265567725__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
/Users/Home/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:145:in `handle'
/Users/Home/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:99:in `rescue in block (2 levels) in start'
/Users/Home/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:96:in `block (2 levels) in start'
/Users/Home/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:86:in `each'
/Users/Home/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:86:in `block in start'
/Users/Home/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:66:in `loop'
/Users/Home/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:66:in `start'
/Users/Home/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/lib/nack/server.rb:13:in `run'
/Users/Home/Library/Application Support/Pow/Versions/0.4.1/node_modules/nack/bin/nack_worker:4:in `<main>'
Help is much appreciated. Thanks!
Here is how i resolved --
I followed the instructions here exactly: https://github.com/plataformatec/devise/wiki/How-to:-Scope-login-to-subdomain
To use these instructions i added the subdomain column to the users table (creating a lot of duplicate subdomains since each team has_many users).
Then in my Team controller when i update a team subdomain it also creates a query to gather all the users associated with the team to update their subdomain values as well. Not the ideal fix but works well enough.
UPDATE
I hope someone finds a better way -- in mean time i'll make a small addition to this. in order for the same email to work across multiple subdomains you need to add the below in your user model. Otherwise the same email will only be registered for 1 subdomain and any registrations on other subdomains will give you the email is already registered error.
validates_uniqueness_of :email, :scope => :subdomain
For this to work you need to remove :validatable from the devise options in your user model and create your own custom validations for email and passwords.
It's not necessary to add column "subdomain" in DB-table. For example in my case I need provide authentication from "www" only for clients, and from "account" only for partners.
def self.find_for_authentication(conditions)
subdomain = conditions.delete(:subdomain)
if user = super
case subdomain
when 'www'
user.client? ? user : nil
when 'account'
user.partner? ? user : nil
else
nil
end
end
end
I guess this approach more simple.
I believe this is actually a bug related to a change in the way ActionDispatch::Request works.
Devise seems to be assuming that if it calls a key on the request hash it will get nil if the value doesn't exist and will get the value otherwise.
It attempts to access the request with .send which doesn't seem to work in the current version of rails. If you patch devise like so:
diff --git a/lib/devise/strategies/authenticatable.rb b/lib/devise/strategies/authenticatable.rb
index 13249e8..df28fc9 100644
--- a/lib/devise/strategies/authenticatable.rb
+++ b/lib/devise/strategies/authenticatable.rb
## -148,7 +148,7 ## module Devise
def request_values
keys = request_keys.respond_to?(:keys) ? request_keys.keys : request_keys
- values = keys.map { |k| self.request.send(k) }
+ values = keys.map { |k| self.request[k] }
Hash[keys.zip(values)]
end
it seems to work fine.
Edit: I wrote up a partial patch with a few more details here:
https://github.com/plataformatec/devise/pull/3965

Simple routing of a controller results in ArgumentErrorr

I have just created a controller using the command:
rails generate controller LegacyPlanCalculator index process
and then I added this to my routes.rb file:
match "legacy_plan_calculator" => "legacy_plan_calculator#index"
match "/legacy_plan_calculator/process" => "legacy_plan_calculator#process"
I can see the controller and its views in the project. The actions in the controller are empty obviously:
class LegacyPlanCalculatorController < ApplicationController
def index
end
def process
end
end
But when I try to goto: http://localhost:3000/legacy_plan_calculator I get this error:
wrong number of arguments (1 for 0)
Rails.root: C:/Users/saadr/Dropbox/workspace/gps4money
I want to be able to land on the index view of the legacy plan calculator when the user puts in localhost:3000/legacy_plan_calculator
UPDATE: Here are the first few lines from 'Full Trace' on the error page:
actionpack (3.1.0) lib/action_controller/metal.rb:193:in `process'
actionpack (3.1.0) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.0) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.0) lib/action_controller/metal.rb:236:in `action'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:29:in `call'
And I don't know where these files are located so can't post relevant code from these files.
process is a reserved word, just change it to any other and it will work.

Can't convert String into integer in ruby/ruby-on-rails

I want to verify if a user exists, and if so, do a password match.
My controller looks like this:
def attempt_login
authorized_user = User.authenticate(params[:username], params[:password])
if authorized_user
flash[:notice] = "Successfully logged in."
redirect_to(:action => 'menu')
else
flash[:notice] = "Invalid username/password"
redirect_to(:action => 'login')
end
end
The model looks like:
def self.authenticate(username="", password="")
user = User.find_by_username(username)
if user && user.password_match?(password)
return user
else
return false
end
end
def password_match?(password="")
hashed_password == User.hash_with_salt(password,salt)
end
In the process of doing this, I receive the TypeError (Can't convert String into Integer). I suspect the error occurs when I want to set a value for authorized_user, but do not know how to approach this issue.
Edit:
My server log is empty. But I can post the Framework Trace:
activesupport (3.0.8) lib/active_support/descendants_tracker.rb:23:in `delete'
activesupport (3.0.8) lib/active_support/descendants_tracker.rb:23:in `block in clear'
activesupport (3.0.8) lib/active_support/descendants_tracker.rb:21:in `each'
activesupport (3.0.8) lib/active_support/descendants_tracker.rb:21:in `clear'
railties (3.0.8) lib/rails/application/bootstrap.rb:59:in `block (2 levels) in <module:Bootstrap>'
activesupport (3.0.8) lib/active_support/callbacks.rb:420:in `_run_call_callbacks'
actionpack (3.0.8) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.3) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.8) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.3) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.3) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.3) lib/rack/lock.rb:11:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.8) lib/rails/application.rb:168:in `call'
railties (3.0.8) lib/rails/application.rb:77:in `method_missing'
railties (3.0.8) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.3) lib/rack/content_length.rb:13:in `call'
rack (1.2.3) lib/rack/handler/webrick.rb:52:in `service'
E:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
E:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
E:/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
And here is the development.log if it could help:
Started POST "/access/attempt_login" for 127.0.0.1 at 2011-06-27 20:19:19 +0200
DEPRECATION WARNING: config.action_view.debug_rjs will be removed in 3.1, from 3.1 onwards you will need to install prototype-rails to continue to use RJS templates . (called from <top (required)> at G:/Projects/basicsocial/app/controllers/application_controller.rb:1)
Processing by AccessController#attempt_login as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jvGVYJEypK9sWoaaa5c2OwzBKmCMX7h7Wp28vBH9wfw=", "username"=>"test88", "password"=>"[FILTERED]", "commit"=>"Log In"}
<-[1m<-[36mSQL (7.0ms)<-[0m <-[1mdescribe `users_pages`<-[0m
<-[1m<-[35mSQL (2.0ms)<-[0m SHOW TABLES
<-[1m<-[36mUser Load (0.0ms)<-[0m <-[1mSELECT `users`.* FROM `users` WHERE `users`.`username` = 'test88' LIMIT 1<-[0m
Redirected to http://localhost:3000/admin
Completed 302 Found in 545ms
TypeError (can't convert String into Integer):
Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (5.0ms)
Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.0ms)
The salt variable is what I use to better secure my user's page. This is something that is stored in the database.
Apparently there was nothing wrong with the code. It was probably related to my browsers cache or something of that nature, as it started working the next day. Sorry for the inconvenience.
I had this same problem and spent hours trying to fix it. It turned out that I had left in a method that the tutorial had told me to delete a few videos beforehand:
def self.hash(password="")
Digest::SHA1.hexdigest(password)
end
In the tutorial, this had been replaced by the methods
def self.make_salt(username="")
Digest::SHA1.hexdigest("Use #{username} with #{Time.now} to make salt")
end
def self.hash_with_salt(password="", salt="")
Digest::SHA1.hexdigest("Put #{salt} on the #{password}")
end
Once I commented this method out, the error went away. I still don't understand why that error showed up in the first place. I added a question and more detail here.
Just want to let you know that i had this problem as well.
The issue was quickly solved but the server showed the error every request afterwards.
Code changes have not been reflected until i restarted the server.
touch tmp/restart.txt was not sufficient so i had to restart the server /etc/init.d/apache2 restart to remove the error message.
$ apache2 -v
Server version: Apache/2.2.16 (Debian)
Server built: Apr 1 2012 07:14:38
$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
$ gem list passenger
*** LOCAL GEMS ***
passenger (3.0.13)
$ bundle exec rails -v
Rails 3.0.7

NoMethodError in Ruby 1.8.7

Ive started working on an application called Shapado (http://shapado.com) which is running on Ruby with a mongo database in the background. Im getting the following error when I try to add some data to the database. I have attached all the relevant files that I think could be the source.
The error
Processing QuestionsController#tags_for_autocomplete to js (for 93.97.246.67 at 2011-03-31 10:02:00) [GET]
Parameters: {"tag"=>"backup"}
NoMethodError (undefined method `find_tags' for #<Array:0x7f24f1032448>):
/var/lib/gems/1.8/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/associations/proxy.rb:109:in `send'
/var/lib/gems/1.8/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/associations/proxy.rb:109:in `method_missing'
/var/lib/gems/1.8/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/dynamic_querying.rb:37:in `method_missing'
/var/lib/gems/1.8/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb:76:in `method_missing'
app/controllers/questions_controller.rb:184:in `tags_for_autocomplete'
app/controllers/questions_controller.rb:180:in `tags_for_autocomplete'
compass (0.10.5) lib/compass/app_integration/rails/actionpack2/action_controller.rb:7:in `process'
warden (1.0.3) lib/warden/manager.rb:35:in `call'
warden (1.0.3) lib/warden/manager.rb:35:in `call'
warden (1.0.3) lib/warden/manager.rb:34:in `catch'
warden (1.0.3) lib/warden/manager.rb:34:in `call'
haml (3.0.25) lib/sass/plugin/rack.rb:41:in `call'
/var/lib/gems/1.8/gems/mongomapper_ext-0.5.0/lib/mongomapper_ext/file_server.rb:17:in `call'
app/middlewares/dynamic_domain.rb:17:in `call'
app/middlewares/mongo_mapper_middleware.rb:24:in `call'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
tags_for_autocomplete method in QuestionsController.rb
def tags_for_autocomplete
respond_to do |format|
format.js do
result = []
if q = params[:tag]
result = current_group.questions.find_tags(/^#{Regexp.escape(q.downcase)}/i,
:group_id => current_group.id,
:banned => false)
end
results = result.map do |t|
{:caption => "#{t["name"]} (#{t["count"].to_i})", :value => t["name"]}
end
# if no results, show default tags
if results.empty?
results = current_group.default_tags.map {|tag|{:value=> tag, :caption => tag}}
end
render :json => results
end
end
/var/lib/gems/1.8/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/associations/proxy.rb:109
protected
def method_missing(method, *args, &block)
if load_target
target.send(method, *args, &block)
end
end
Replace "current_group.questions.find_tags" with "Question.find_tags" in the 2 locations in code.
As mentioned here:
http://shapado.com/questions/what-is-the-cause-of-the-nomethoderror-when-using-the-search-box-on-the-tags-page
This seems to work for me.

Resources