Rails staging environment says "undefined method" but the console shows that method fine - ruby-on-rails

Anyone have this happen before? I just uploaded my app to a staging server that's identical to the production server. I try to log in, and I get the "Something went wrong" 500 page. The log tells me the error is in my SessionsController (using restful_authentication), with the specific error being NoMethodError (undefined method 'enabled?' for #<User:0xb6c5dbd4>):. I load up a console for my staging environment and pull back the user in question... and the enabled method is defined (calling user.enabled? returns the expected value, and not a no method error).
Anyone have any ideas? I've specified in Passenger that the RailsEnv should be staging; could it be that Passenger is not recognizing this? When I created my database and ran my migrations, I had to explicitly say RAILS_ENV=staging, otherwise it was trying to load the development data and throwing an error because my development box connects through a different MySQL socket than staging|production.
The enabled method is defined on my users table and therefore the User model. The code in the controller is something like this:
# SessionsController.rb
def create
user = User.authenticate(params[:username], params[:password)
if user
unless user.enabled?
note_disabled_account # basically prints a message
else
self.current_user = user
# other stuff
redirect_back_or_default('/')
end
end
end
The code is identical in Production and works fine, so the issue is something related to the fact that I created a "staging" environment.
EDIT: I've narrowed the error down to being some kind of caching issue, despite the fact I've restarted Passenger. We are using Amazon EC2 and the staging server was basically cloned from Production (so it had an older version of the application on it). For some reason when I am deploying from Capistrano and restarting Passenger, it does not seem to properly link to the new version; I tried removing the enabled line and I was able to log in, but pulling up a record gives me another undefined method - the method in question is something that was added after the Staging server was created.

Related

Ruby, ActiveRecord: Local rails server updates record attributes whereas the remote server does not

I have a script which sends data from a Windows back end to Heroku. Recently I added four new columns to the object model. For testing each of the properties is updated individually through a cURL request.
On the local rails server (which is identical to the Heroku hosted server) all four new attributes are populated correctly and there are no errors in the logs.
On the Heroku based server when I run the back end script it updates two of the columns but not the other two. In spite of these values not being populated the logs indicate that the process ran successfully with status of 200.
Has anyone experienced something similar to this?
Ok, managed to resolve this issue.
The solution was to run heroku restart. Seemingly since Heroku apps are precompiled the new columns were not being recognised by the app.
Peculiarly, the app should've probably raised an UnknownAttributeError before the fix was applied.
Cheers,
CBusBus.

Reset database causes heroku custom domain to issue 404

I added a custom domain to my heroku app sometime last week and its been pointing well up until this last night.
A huge error made me reset my database using the heroku pg:reset DATABASE_URL command.
Since then, attempts to visit my app from the custom domain issues a 404, page not found error.
However, when I visit the app directly through its heroku domain name, it works without issues.
I've tried restarting the server, re-adding the domain name even after confirming its there (using heroku domains).
I'd be glad if anyone could help with this error. Thanks.

Rolify dynamic calls not run or recognized by heroku

I am getting an error for using a function provided by a special configuration on Heroku even though it works fine on my machine.
Specifically, I am using the Rolify method is_admin? on user, which is made possible by a configuration setting in an initializer file.
So far with Heroku I have migrated the database again, restarted the app and refreshed the page multiple times for each. How can I get Heroku to run the configuration file like my local machine does every time I reset the local server?
Error:
ActionView::Template::Error (undefined method `is_admin?' for #<User:0x0000000524c8e0>):
2012-07-22T09:00:53+00:00 app[web.1]: 1: <% if current_user.is_admin? %>
Code:
# config/initializers/rolify.rb
Rolify.configure do |config|
config.use_dynamic_shortcuts
end
Well, I tried one more thing and it worked, but here it is just in case.
I ran $ heroku run rake db:reset and it worked again. Not sure why, but oh well.
Edit:
The issue is that if you call Rolify custom methods when the role does not exist yet in the database, it results in an error. To get around this, either seed your database with all of the roles you'll need, or better, do not use dynamic methods.
For example, instead of:
#user.is_admin? use #user.has_role?(:admin) and it will not break.

rails admin_data showing development db instead of production

ive successfully deployed admin_data onto heroku.
i can visit
myapp.herokuapp.com/admin_data
and the admin_data page shows. however it shows my development db. what's worse is that it actually shows my old entries (no longer exist).
is it possible for admin_data to show the production db?
i have an admin.rb file with the following...
AdminData.config do |config|
config.is_allowed_to_view = lambda {|controller| return true if (Rails.env.development? || Rails.env.production?) }
end
am i missing something?
ive looked at the https://github.com/bigbinary/admin_data/wiki but cannot seem to find anything about making the production db show up
thank you!
UPDATE:
sorry i must be mistaken but... isn't visiting myapp.herokuapp.com my production site? and if i create a user there, isn't it being stored on herokus pg database? i was trying to view that database, and i thought going to myapp.herokuapp.com/admin_data would display that while going to localhost:3000/admin_data would show the users from the db on my local machine.
how do i view the database thats storing the user created from heroku with admin_data?
A Heroku application only has one database attached to it. If you're viewing the pages via a Heroku URL then you are viewing whatever database that application is using. You don't have multiple development/staging/production databases.

Rails 3.2 has_secure_password fails silently when deployed to Heroku

I upgraded the authentication in my application to use Rails 3.1's has_secure_password facility. In the process, I created a page to allow users to change their passwords. I tested it and it works on my development machine, both in development and production environments.
When I deployed the application to Heroku, I went to try it and it seemed to work, except when I logged out and logged back in, my password was unchanged. I tried changing the password manually in the console and that works fine. If I try to enter different text for the password and confirmation, it shows the validation it is supposed to, which means the password is getting sent to the app correctly.
Here is the relevant change to my controller: https://github.com/mjm/sis-lunch/commit/930ced467a0e23ad48f4497999183112c5f846b1#diff-2
Is there something I'm missing? What could be wrong with it in production on Heroku that could cause this to silently fail?
I'm not sure how you are testing it on your development machine, since PeopleControllerTest is empty, but the password field is protected against mass assignment. It shouldn't work in PeopleController the way it is written. (that's a good thing!)
You will need to explicitly call Person#password= in your controller.
The relevant Rails source code for ActiveModel::SecurePassword can show you exactly what happens when you use has_secure_password.
I believe I figured it out. I deployed the app to Heroku, then ran the migrations. The app was not fully aware of the new password_digest column, but new consoles were, so they worked fine. Restarting the app using heroku restart fixed it.

Resources