Rails 3.2 has_secure_password fails silently when deployed to Heroku - ruby-on-rails

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.

Related

How to add a user to a rails app manually?

I've been assigned to work on a legacy Rails app, and nobody has a development environment working. I've gotten everything up and running, but I can't create a new user on my development environment because the facebook api doesn't like the api call originating from an IP that isn't the production server, and the mailer for the non facebook signup times out for the same reason. I've narrowed it down to a couple options:
Figure out a way to get the facebook api to accept the request from my development box
Fix the mailer or wrap it in a conditional so that email confirmation is not required in the development environment
Manually add a user to the database
Which option is best, and how would I go about doing it? I'm relatively new to rails and backend programming. Options one and two seem like potential security risks, but I have no idea how to go about doing option 3. I tried adding User.create([{email: '(redacted)'}, {password: '(redacted)'}]) to the seeds.rb file and running db:seed with no luck.
Suggestions?
Your easiest option is definitely simply to create the new user manually. Instead of launching your rails server ($ rails s), launch your rails console ($ rails c). From this command line (which is identical to the debugger, if you've ever used that before), you may create a user and save it. You will still be subject to the validations in the User model, but something along the lines of the following should work:
new_user = User.new( email: 'myemail#example.com', name: 'Joe Smith' )
new_user.save
As long as you match the validations of the model, and connect to the appropriate database, this is a very simple approach. To connect to the right database, either update config/database.yml so that your development environment is the one you want to hit, or run specifically in the correct environment (if it's production, as you implied, simply run $ rails c RAILS_ENV=production instead of the aforementioned$ rails c).

Devise use different email template in production and development

I customized devise email templates under
views/devise/mailer/ and everything worked fine in development mode.
When I switched to production mode, devise sends email with the default email templates instead of the ones I customized.
Anything that might cause this?
Thanks!
I solved this by restarting my sidekiq. Apparently sidekiq had a cache of the original email template.

Heroku rails blogger not processing login

So I push my rails_blogger app to Heroku and it goes fine, I run rake db:migrate and it goes fine, but then when I try to login using the email and password I made, it doesn't let me login and just renders the login page again.
When I run webrick on my computer everything works fine with login, but when I access the app from the heroku URL in my browser, it won't let me login!
Also, it won't even show the articles on the main page that anyone should see, logged in or not.
I've looked all over various google search results to find the answer to this to no avail.
Can someone help me understand the problem here?
GitHub repo: https://github.com/ck626/project_rails_blogger
Heroku page: scary-goblin-6551.herokuapp.com
The database that you use at heroku are different from your local database, so you need to create authors separately on heroku and at your local development server. In Rails you can configure your database (what database to use at production and at development) at config/database.yml.
Another thing to note heroku does not support sqlite, you need to use pg instead.

Rails Tutorial: Why can I sign up a new user without problems on the local server but not on Heroku?

I've completed the Rails Tutorial. I kept up with every detail until the end of chapter 8. The last four chapters I just copied and pasted without trying to understand much.
The Sample App works perfectly on a Cloud9 local web server, but not on Heroku. Specifically, When I want to sign up a new user, I get the message "We're sorry, but something went wrong."
Why can I sign up a new user without problems on the local server but not on Heroku? Is the functionality of the final sample app supposed to be exactly the same on both local and heroku?
You are getting a 500, look at the server logs for a clue. Probably need to run migrations.
Run your migrations:
heroku run rake:db migrate
You may need to restart heroku:
heroku restart
If it still doesn't work, try resetting your database. If you're using postgres, try:
heroku pg:reset DATABASE
I completed every step in the entire Rails Tutorial book, except one, which I thought was optional and wouldn't affect the result in production.
10.3 Email in Production shows you how to configure your application to send emails in production (for account activation in the book's sample application) using a Heroku add-on called Sendgrid.
You do have to give Heroku credit card information (though for the purposes of the book you don't have to actually make any purchase or subscription), and this is why I initially didn't implement this section of the book.
After following along and implementing section 10.3 my final application is fully functional in production.

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.

Resources