Rails & Heroku - Find User By Lowercase Username - ruby-on-rails

in rails development environment I find a user by its lowercased username in the routes.
For example:
#user = User.find_by_username(params[:id].downcase)
This piece of code runs perfectly fine in development, but when this code runs in production (Heroku) I get
ActionView::Template::Error (undefined method `username' for nil:NilClass):
Is there a go around for this issue?

By the end I've used
User.where('lower(username) = ?', params[:id]).first
Sorry for the consfusion and the incertitude :)

Related

Rails.application.assets_manifest.find_sources("#{params[:controller]}.js").any? crashes on Heroku

in my application_layout.html.rb of my rails application I'm loading controller specific javascript:
<%= javascript_include_tag params[:controller] if Rails.application.assets_manifest.find_sources("#{params[:controller]}.js").any? %>
Which is working correctly until I deploy to Heroku. Than I get this error:
ActionView::Template::Error (undefined method `find_asset' for nil:NilClass)
The same error I get when I try to call any method on the object returned from #find_sources.
EDIT:
<%= Rails.application.assets_manifest.find_sources("#{params[:controller]}.js") %>
returns #<Enumerator:0x007fda655a17b0>
Any idea?
Thanks
It does not work because there is a static manifest file in production and no in development (usually, it depends on environment config). You can create a helper method to do the check differently based on current environment configuration:
def asset_exist?(path)
if Rails.configuration.assets.compile
Rails.application.precompiled_assets.include? path
else
Rails.application.assets_manifest.assets[path].present?
end
end
Please check this issue for more details.

How to access "extra" Hashie in omniauth-facebook callback

In a Rails app I had the following code updating the user's profile from their Facebook profile
self.update_attributes( :location => request.env["omniauth.auth"].extra.raw_info.location.name )
I'm in the process of updating Rail and gems, and now this line is failing.
NoMethodError (undefined method `name' for nil:NilClass):
Looking at the output, I see
...
extra: !map:Hashie::Mash
raw_info: !map:Hashie::Mash
...
How can I access attributes within this Hashie?

NoMethodError (undefined method `current_user=' heroku and devise

Can't work out what I've done to mess this up. It works on localhost but not when I deploy any more. I have migrated the database and restarted heroku but it's still not working.
My heroku log is
NoMethodError (undefined method `current_user=' for #<Class:0x000000044ac488>)
current_user is referenced many times the app and has previously worked fine. And this is the same error when trying to load many different pages. It's not pointing me to a particular action...
This happened to me because I accidentally removed the devise route:
devise_for :users, controllers: {registrations: "registrations"}
end

Rails 3 - Delayed_Job (collectiveidea), trying to Delay Mailers - Error: NoMethodError (undefined method `delay' for UserMailer:Class)

I'm using the delayed_job gem here: https://github.com/collectiveidea/delayed_job
I have the following in an observer:
UserMailer.delay.msg_notification(record)
In user_mailer.rb
class UserMailer < ActionMailer::Base
...
def msg_notification(record)
mail(
:to => "#{record.user.email}",
:subject => "Notification"
)
end
..
end
But this errors with:
NoMethodError (undefined method `delay' for UserMailer:Class):
Any ideas? thanks
I've seen a problem like this on our Rails app (2.3.8, but the issue sounds the same). Basically, there are three ways to delay an action:
MyClass.delay.foo(arg)
Putting handle_asynchronously :foo in your class definition after the definition of foo
MyClass.send_later(:foo, arg)
For whatever reason, #3 was the only form that worked consistently across all our development machines. #1 died on our development server (Ubuntu); #2 on our designer's Mac. But #3 was fine.
Hope that helps!
Also check if you have restarted your server after the bundle install. That could be a issue too...

will_paginate works on local box, gives "undefined method to_i" on server but both point to the same database

We just ran into this problem the other day all of a sudden. Our Rails app is using will_paginate, and I have it defined as follows in my controller:
# contacts_controller.rb
def index
# ...
#search = #current_user.contacts.search(params[:search])
#contacts = #search.all.paginate({:page => params[:page], :per_page => 20})
end
Both development and staging (this is a staging box not production) point to the same database. The above code works fine on my local machine, however on staging I get the following error:
undefined method 'to_i' for {:per_page=>20, :page=>nil}:Hash
The code is identical on both computers. Any idea why it works fine on one and not on the other?
EDIT: On the staging server I was using the plugin while on my local box I was using the gem, however I removed the plugin and installed the gem and now I get an error that says:
uninitialized constant Array::WillPaginate
Evidently it is still not solved.. after doing a deploy on the staging server I'm getting that error again, despite the fact that it was working before fine. Anyone have any ideas at all on this? I'm totally clueless.
If the page param is nil, it's often a good idea to default to a value of 1. This covers cases where no url parameter for page is passed. Where a parameter is passed, it will be used in place of the default:
{:page => params[:page] || 1, :per_page => 20})
Full example:
# contacts_controller.rb
def index
# ...
#search = #current_user.contacts.search(params[:search])
#contacts = #search.all.paginate({:page => params[:page] || 1, :per_page => 20})
end
Well, I seem to have solved it:
I had to explicitly type require 'will_paginate' in the environment after I uninstalled the plugin and installed the gem. However I'm still not sure why the plugin was working on my local machine but not on staging.

Resources