Bug when attempting to rake db:reset - ruby-on-rails

I am attempting to setup an admin account for my first rails app.
This is the code I used to create the admin account:
admin = User.new(
name: 'Admin User',
email: 'admin#example.com',
password: 'helloworld',
password_confirmation: 'helloworld')
admin.skip_confirmation!
admin.save
admin.update_attribute(:role, 'admin')
Here is the code in question that is failing in Sublime:
50.times do
Post.create!(
user: users.sample,
topic: topics.sample.
title: Faker::Lorem.sentence
body: Faker::Lorem.paragraph
)
end
In terminal I am receiving this error message:
rake aborted!
SyntaxError: /Users/Alex/Desktop/code/Bloccit/db/seeds.rb:39: syntax error, unexpected ':', expecting ')'
title: Faker::Lorem.sentence
^
/Users/Alex/Desktop/code/Bloccit/db/seeds.rb:40: syntax error, unexpected ':', expecting keyword_end
body: Faker::Lorem.paragraph
^
/Users/Alex/Desktop/code/Bloccit/db/seeds.rb:41: syntax error, unexpected ')', expecting keyword_end
When I added the admin account, it appeared to add fine but after continuing on with my assignment I needed to log in with the admin account. After attempting it, it states the login information was incorrect. So I wanted to reset the DB to start over and this is where I am at now. Please help.

You missed comma separators after topic: topics.sample and title: Faker::Lorem.sentence.
50.times do
Post.create!(
user: users.sample,
topic: topics.sample, # <~~ Here
title: Faker::Lorem.sentence, # <~~ Here
body: Faker::Lorem.paragraph
)
end

Related

Using Faker Gem to Seed Data

I'm attempting to seed my database using the Faker gem, but am getting some error messages and can't see where I'm going wrong. My seeds.rb is:
10.times do
List.create! (
name: Faker::Company.buzzword,
shared_with: Faker::Internet.email,
user_id: 3
)
end
50.times do
Item.create! (
name: Faker::Company.bs,
list_id: Faker::Number.between(1, 10),
delegated_to: Faker::Internet.email,
user_id: 3
)
end
puts "Seed finished"
puts "#{List.count} lists created"
puts "#{Item.count} items created"
And the error messages are:
rake aborted!
SyntaxError: /Users/.../db/seeds.rb:3: syntax error, unexpected tLABEL
name: Faker::Company.buzzword,
^
/Users/.../db/seeds.rb:4: syntax error, unexpected tLABEL, expecting '='
shared_with: Faker::Internet.email,
^
/Users/.../db/seeds.rb:5: syntax error, unexpected tLABEL, expecting '='
user_id: 3
^
/Users/.../db/seeds.rb:11: syntax error, unexpected tLABEL
name: Faker::Company.bs,
^
/Users/.../db/seeds.rb:12: syntax error, unexpected tLABEL, expecting '='
list_id: Faker::Number.between(1, 10),
^
/Users/.../db/seeds.rb:12: syntax error, unexpected ',', expecting keyword_end
/Users/.../db/seeds.rb:14: syntax error, unexpected tLABEL, expecting '='
user_id: 3
^
/Users/.../db/seeds.rb:20: syntax error, unexpected end-of-input, expecting keyword_end
Can anyone clue me in to where I'm going wrong?
In Ruby you should never put whitespace between a method name and the opening parenthesis.
# Syntax error
List.create! (
# Correct
List.create!(
So to expand, your code should look like:
10.times do
List.create!(
name: Faker::Company.buzzword,
shared_with: Faker::Internet.email,
user_id: 3
)
end

Problems with 'seeding' data to different users

I would like to seed some data to my wiki model user the 'faker gem'. I have created three users and would like to 'spread' 20 wikis over them.
I installed the faker gem, run bundle and set up my seedfile like this:
require 'faker'
# Create an admin user
admin = User.new(
email: 'admin2#example.com',
password: 'helloworld',
role: 'administrator'
)
admin.save!
# Create a moderator
moderator = User.new(
email: 'moderator2#example.com',
password: 'helloworld',
role: 'moderator'
)
moderator.save!
# Create a member
member = User.new(
email: 'member2#example.com',
password: 'helloworld'
)
member.save!
users = User.all
15.times do
Wiki.create!(
title: Faker::Lorem.sentence,
body: Faker::Lorem.paragraph
user: users.sample
)
end
wikis = Wiki.all
puts "Seeds finished"
If I run this I get an error:
SyntaxError: /Users/marcvanderpeet/Projects/bloc/blocipedia/db/seeds.rb:37: syntax error, unexpected tIDENTIFIER, expecting ')'
user: users.sample
I dont understand why I get this error as when Im running rails c I can just type in a user:
2.1.5 :001 > u = Wiki.new
=> #
Any clues on how to fix this?
You have a syntax error: the line
body: Faker::Lorem.paragraph
should end with a comma.
Already got it, forgot a ','. Devil is in the details!

trying to create a new rails admin user on heroku console

In the heroku console I'm entering
Admin.create(email:‘me#gmail.com’,username: ‘Me’,password:’pass’,password_confirmation:’pass’)
When I run this I get
SyntaxError: (irb):16: syntax error, unexpected tIVAR, expecting keyword_do or '{' or '('
...reate(email:‘me#gmail.com’,username: ‘Me’,p...
... ^
(irb):16: syntax error, unexpected tLABEL, expecting '='
...me#gmail.com’,username: ‘Me’,password:’pass...
Can anyone help with what's wrong with my Admin.create code?
Thanks
Looks like dodgy quotation marks, or spacing? Does this work? (copy & paste)
Admin.create(email: 'me#gmail.com', username: 'Me', password: 'pass', password_confirmation: 'pass')

Agile Development on Rails: Can someone explain this syntax? image_url: fred.gif)

Newbie question here:
In this test:
test "product is not valid without a unique title -il8n" do
product = Product.new(title: products(:ruby).title),
description: "yyy",
price: 1,
image_url: "fred.gif")
end
Why is there a ")" (closing parenthesis) after "fred.gif"? There is no opening parenthesis so I do not understand the logic around it. Can't find any reference on why either.
Looks like an error to me, it's not valid syntax:
$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
$ ruby -c /tmp/test.rb
/tmp/test.rb:3: syntax error, unexpected tLABEL
description: "yyy",
^
/tmp/test.rb:3: syntax error, unexpected ',', expecting keyword_end
/tmp/test.rb:5: syntax error, unexpected ')', expecting keyword_end
I'd guess the line
product = Product.new(title: products(:ruby).title),
should read
product = Product.new(title: products(:ruby).title,
so reformatted, the test would look like this:
test "product is not valid without a unique title -il8n" do
product = Product.new(
title: products(:ruby).title,
description: "yyy",
price: 1,
image_url: "fred.gif"
)
end
(though the test description does not match the behaviour ;-))

Nested attributes in rake task

I'm trying to create a rake task to populate my DB. I need to to create 1 user with a specifc set of deatails and 99 other randomly genraterated . The details that are required are for two Models at the same time, similar to a form with nested attribues. My model uses an after_create call back to create and link a users to a team. The team name by default is the users name
the current code that I have used it producing errors
sample_data.rake
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
user = User.create!(:profile_attributes => { name: Forgery::Name.full_name },
email: "test#example.com",
password: "123qwe",
password_confirmation: "123qwe")
99.times do |n|
:profile_attributes => { name: Forgery::Name.full_name },
email = Forgery::Internet.email_address
password = "password"
user = User.create!(email: email,
password: password,
password_confirmation: password)
end
end
end
Model that relys on the nested attibute.
class User < ActiveRecord::Base
after_create :set_user_on_team
private
def set_user_on_team
self.create_owned_team(:name => self.profile.name ).users << self
end
end
error message
rake aborted!
/lib/tasks/sample_date.rake:9: syntax error, unexpected tASSOC, expecting keyword_end
:profile_attributes => { name: Forgery::Name.full_name },
^
/lib/tasks/sample_date.rake:9: syntax error, unexpected ',', expecting keyword_end
I noticed some syntax errors in the 99.times block. You could try it like this:
99.times do |n|
profile_attributes = { name: Forgery::Name.full_name }
email = Forgery::Internet.email_address
password = "password"
user = User.create!(profile_attributes: profile_attributes, #my assumption
email: email,
password: password,
password_confirmation: password)
end

Resources