How to add a user to a rails app manually? - ruby-on-rails

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).

Related

sending and receiving activation emails - ruby on rails tutorial by michael hartl

I recently started following Hartl's Ruby on Rails tutorial.
I downloaded the existing sample_app_reference app from bitbucket.
My goal is not to start and learn everything from scratch but rather to add new features to the already existing app.
The issue is that I can't test the features I'm adding since I can't activate my account locally. In order to activate my account, I need to receive an activation email (which I'm not). I've searched the web for hours, but couldn't find anything regarding the issue. Did anyone try this tutorial before and encountered the same problem? Any help would be much appreciated.
Yeah...sending emails from dev environment is often not straightforward. As Vasilisa comments above, the quickest fix for your need is to activate the user from the rails console...perhaps like this:
% bundle exec rails console
my-app(development)> User.last.activate
However, there will be other times when you actually want to see the email in the context of a mail client. For this, https://mailcatcher.me/ makes it super easy. Just install the mailcatcher gem -- not in your app's Gemfile, but on your local machine's ruby installation.
Then, change the host and port in app/config/development.rb (NOT in other environemnts, like prod!)
config.action_mailer.smtp_settings = {
# For use with Mailcatcher: https://mailcatcher.me/
address: '127.0.0.1',
port: 1025
}
Run mailcatcher once from a command prompt.
Then, do your thing in your app to trigger an email send, and open http://127.0.0.1:1080/ in your browser. Voila, you see your email like it was really sent.
This a super-easy, super-useful way to see real emails without setting up your local env to actually send emails.

Method for reseting all users in database with Ruby on Rails 4

So I've set up a new authentication process within my website using rails (rails version '4.2.0', sqlite3 database manager, ruby 2.1.1p76) on OSX Mavericks (yes, I still haven't updated yet!). The website as yet is still only local as I'm learning rails.
I have modified the user authentication to utilise a cookies remember me function, but old users can not login, i.e. error Couldn't find User (ActiveRecord::RecordNotFound in WelcomeController#homepage)
Newly created users can.
I was just wondering what the terminal command is to somehow to delete all records of users from the database, so that all new users need to sign up to obtain the remember me functionality...? I can't seem to find the exact answer and I don't want to start messing too much with the database.
Start a rails console by typing
rails c
in the terminal, in your rails application folder (not the app subfolder).
type
User.destroy_all

Created an entry in rails console, but app has no idea it exists

I entered
SiteData.create(site_name: "My Site", url: "http://mysite.com")
in the console, SiteData being one of my models. Now I can write SiteData.find(1) and it will echo back the entry. Great!
I'm using the db table to store all the info about my site, such as the name, the url, the Facebook page, the Google Plus pages, etc. SO I need to access it on every page. Okay, I'll use before_filter in the application controller to make this work, right?
So I went into my app, and in the Application controller, I wrote
before_filter :add_site_data
def add_site_data
#site_data = SiteData.find(1)
end
And I get an error: "Couldn't find SiteData with id=1"
What now????
This might not be the best way to do this. I am brand new to rails. But I'm totally stuck and have no idea what's going on or why here. I installed Postgres locally today and set up the test and dev dbs, but it seems to be working fine since I can still create/read/update/destroy entries from the console.
Please help! I'm going nuts here. I am using Rails 4.0.
Resetting the server fixed it.
If you know you're making entries in your database that aren't reflected when you run your app, reset the server.

How can I add the first user to a Rails application that uses Authlogic, Devise, etc.?

I have a Ruby on Rails application which I cloned from a Git repo. It requires a login to do anything, such as register a new user. There are currently no registered users so I see myself as completely locked out
I know the users are stored in a table called users but the underlying database is MySQL and I don't know how to access the database of a Ruby on Rails application.
Does anyone know how I would go about adding a user?
This is quite a 'classic' problem and the immediate thought ("just do it mySQL") doesn't work here because of the need to have the rails piece that encodes a password that is entered.
So you need to actually use rails, something like this (this should all be happening in your local development environment which is the default when working locally):
You need to create a user.
Try this:
cd the_root_of_the_project
script/rails console
> User.create(:username => 'admin',
:password => 'abc123',
:password_confirmation => 'abc123')
# Add other fields, such as first_name, last_name, etc.
# as required by the user model validators.
# Perhaps :admin => true
This assumes a few things (so change as required) such as an authentication system such as authLogic or devise, attribute and field names, etc, but you should be able to adjust to your needs. You can determine what these are by looking at a few things, specifically the database migration files in db/migrate, the model validations in user/model/user, any existing "seeds" filew for users in db/seeds.rb and the authentication system hooks.
As to 'where' to do this - obviously the console works but you might also want to use the seeds file for this. Whatever 'create' command you use in the console can be placed in here, then run with rake db:seed. The downside is that if you check this file into source control it's less secure. The seeds file is really useful for other tasks such as creating reference tables, initial categories, etc.
If you don't have the database actually created at all yet, you'll need to be aware of and use these tasks:
rake db:create
# as it sounds, creates a database (but no application tables or columns),
# using the config/database.yml file for the connection info.
rake db:migrate
# Creates tables and columns using the db/migrate/ files.
rake db:seed
# Runs commands in db/seeds.rb to create initial records for the application.

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