Application with only one user admin without sign up part - ruby-on-rails

I did all Michael Hartl tutorial's application, and now I would like to create my on one, a writer assistant to edit a book.
I want to have only one user in database and it will be the admin. In the menu, only a signin button to connect and begin writting the book.
I'm wondering how to do and what is the best way to put only one user in the database. As in the tutorial there is the sign up part.
I hope you understand what I mean, and sorry for my bad english
Thanks

If you did want database manipulation/seeding
you could either
a) add a create statement in the seed.rb file
admin_user = User.create!(first_name: "test",
last_name: "test",
password: "123",
password_confirmation: "123",
roles: ["admin"])
then from the command line within your project run
rake db:seed
b) From the command line run
rails console
This gives you a command line loaded rails environment.
Where you can execute
admin_user = User.create!(first_name: "test",
last_name: "test",
password: "123",
password_confirmation: "123",
roles: ["admin"])

Related

How to configure Cucumber in Rails 4

I'm trying to use Cucumber in a Rails 4. I've added the Cucumber-Rails gem, followed the steps in the instructions but when I wrote step definitions like so:
When(/^I submit a sign up with the following:$/) do |table|
user = User.create({
first_name: 'Name',
last_name: 'Last',
email: 'email#example.com',
domain: 'example.com',
password: 'foobar',
password_confirmation: 'foobar'
})
end
I get the following error: uninitialized constant User (NameError)
./features/step_definitions/users/sign_up_steps.rb:2:in/^I submit a sign up with the following:$/'
features/users/sign_up.feature:4:in When I submit a sign up with the following:'
What am I missing?
Please check , it's looking like you have missed the User model
User model is missing for which you are running tests.

what is the seeds.rb in db folder in Ruby on Rails ?

what is the relationship between db/seeds.rb and db/schema.rb? And How do I use them ? by the way,
# db/seeds.rb
Todo.create!(title: 'grocery shopping', notes: 'pickles, eggs, red onion')
Todo.create!(title: 'wash the car')
Todo.create!(title: 'register kids for school', notes: 'Register Kira for Ruby Junior High and Caleb for Rails High School')
Todo.create!(title: 'check engine light', notes: 'The check engine light is on in the Tacoma')
Todo.create!(title: 'dog groomers', notes: 'Take Pinky and Redford to the groomers on Wednesday the 23rd')
I can't understand 'notes'
The seed.rb helps you to initialize data into your db.
You can run the file by:
rake db:seed
or run
rake db:setup
to create db, run migrations, and run seed
Let's say you create a db and you can fill it with seeds.rb. In other words, you fill your tables in accordance with the model you generated beforehand.
User.create (username: "tony", password: "12345666")
User.create (username: "Clara", password: "fdvfdvfd666")
User.create (username: "Hans", password: "1gbfdbg2345666")

How to resolve "not authorized for query on db.users" Mongoid

I've created a new Rails app using Rails 4 and Mongoid 4. I'm getting this error "not authorized for query on mydb.users" when I run my test:
RSpec.describe User, type: :model do
it "is invalid without a name" do
user = User.new(name: nil)
user.valid?
expect(user).to be_invalid
end
...
end
At first I thought this was an authorisation issue with Mongoid and MongoDB, but I can access mongoldb/db/collection without authenticate in the console without trouble.
Because I'm new to Rspec tests, I'm not sure the problem is with my test, mongoid or mongodb.
Any ideas?
MongoDB provide access on database level and it store on database level also. You need to provide access on this database like :
use mydb;
db.createUser(
{
user: "accountUser",
pwd: "password",
roles: [ "readWrite", "dbAdmin" ]
}
)

Validation failed on rake db:seed

I'm doing chapter 12 of hartle's tutorial. When I ran bundle exec rake db:seed I got this error:
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
I try running
rake db:reset
rake db:migrate
rake db:test:prepare
And at last
rake db:populate
but they didn't solve the problem. When I run rake db:populate it gives:
Don't know how to build task 'db:populate'
This is my seeds.rb file:
# Users
User.create!(name: "Example User",
email: "example#railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true,
activated: true,
activated_at: Time.zone.now)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}#railstutorial.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password,
activated: true,
activated_at: Time.zone.now)
end
# Microposts
users = User.order(:created_at).take(6)
50.times do
content = Faker::Lorem.sentence(5)
users.each { |user| user.microposts.create!(content: content) }
end
# Following relationships
users = User.all
user = users.first
following = users[2..50]
followers = users[3..40]
following.each { |followed| user.follow(followed) }
followers.each { |follower| follower.follow(user) }
I guess maybe the problem is with this line email = "example-#{n+1}#railstutorial.org"
Your problem is that rake db:reset not only drops and recreates the database, but it also migrates and seeds it as well. So essentially what's happening is this:
rake db:drop
rake db:create
rake db:schema:load # (think of this as running all the migrations you've run before)
rake db:seed # (creates your 100 database users)
and then you run:
rake db:migrate # (likely unnecessary, but it causes no harm)
rake db:test:prepare # (prepares the test database)
rake db:prepare # (runs the seeds AGAIN and causes your errors)
Obviously, from this if you just stop running the rake db:prepare command your problem will go away. However, to avoid these things in the future, I strongly recommend putting a little bit of logic in your seed file. It's just Ruby, so you could wrap the User creates in an unless statement, such as:
unless User.find_by( email: "example#railstutorial.org" )
# create all 100 users
end
This will prove to be especially valuable if you have a site on production that still uses seed data (such as a SiteSetting table); you need to make sure the data makes its way into your production database, but you'll create duplicate records (or errors) running the seed again without dropping.
As an additional reference for the answer to your question, see the selected answer to this one.
I hope this provides all the information you need!
I'm doing chapter 12 of hartle's tutorial. When I ran bundle exec rake
db:seed I got this error:
ActiveRecord::RecordInvalid: Validation failed: Email has already been
taken
When you run rake db:reset, it will seed the database for you. When you then run rake db:seed, an exception will be thrown, because you are using create! in your seeds.rb file. Unlike create, create! raises an exception when the validations fail.
You can check this by running rake db:reset, and then using rails console to check your database entries.
There are a couple things you could do to prevent this, but why would you, when your data is already there?
When I run rake db:populate it gives:
Don't know how to build task 'db:populate'
Unless you define it yourself, there is no rake task named db:populate.
try using:
if the case is already existing email it will solve it.
email = "example-#{rand(100000)}#railstutorial.org"
and you can also see errors:
user = User.new(name: "Example User",
email: "example#railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true,
activated: true,
activated_at: Time.zone.now)
user.errors
user.save if user.valid
Do you have both faker and populator installed in your Gemfile? That is most likely apart of the issue. Make sure you have run:
gem install populator #From the command line
and include it in your Gemfile:
gem 'populator'
Here is a link to the Git repo https://github.com/ryanb/populator/tree/master
Great article here also: http://sudharti.github.io/articles/using-faker-and-populator-rails/

ruby-on-rails: Seeding-data strategies (or loading test data into developer database)

I want to clear and re-load my developer database (Ruby on rails) frequently.
Of course, I can manually add the data via the web page but I was wondering if anyone has any strategies for this type of testing.
(I already have unit, functional and integration tests, fyi)
Thanks
Create a seed.yml file in db directory. Add a YAML document for each model you want to create. This document should contain a list of hash. Each hash should contain model attributes.
users:
- login: jake
password: jake123
password_confirmation: jake123
first_name: Jake
last_name: Driver
- login: Jane
password: jane123
password_confirmation: jane123
first_name: Jane
last_name: McCain
categories:
products:
In your seed.rb file
seed_file = File.join(Rails.root, 'db', 'seed.yml')
config = YAML::load_file(seed_file)
User.transaction do
config.keys.each{ |key| key.classify.constantize.create(config[key]) }
end
I find it easier to modify the seed data in the YML file. Application that I have built is deployed by a different team. They like this approach too.
To clear the data I have a rake task in lib\tasks directory. I run the rake task as app:flush.
namespace :app do
desc "Flush all the seed data "
task :flush => :environment do
config = YAML::load_file(File.join(Rails.root, 'db', 'seed.yml'))
User.transaction do
config.keys.each{ |table| truncate_table(table)}
end
end
end
Time to look at "fixtures" and "seeding data" ;-) I am not good enough to give you a clear explanation, but googling those two keys should give you all you need.
Check these out: http://derekdevries.com/2009/04/13/rails-seed-data/
http://lptf.blogspot.com/2009/09/seed-data-in-rails-234.html

Resources