I want to link to localserver:3000 on ruby on rails but when I use bundle exec rails server, it just exits and doesn't generate the server.
➜ demo git:(master) ✗ bundle exec rails server
=> Booting Puma
=> Rails 5.2.0 application starting in development
=> Run `rails server -h` for more startup options
Exiting
This is my route.rb if it helps!
Rails.application.routes.draw do
root "pages#home"
end
I created a controller called pages and created a view called home. When I first generated the server, I already ended the session so I'm wondering how I can generate the server again so I can see my "home" page.
This is what happens when I try to get the server. Can anyone explain to me what could be wrong? Thank you!
Edit:
I created my controller by using
bundle exec rails g controller pages
Then I added home.html.erb under views.
I managed to connect to the server but this is the error I see on my page:
I ran the command but nothing happened. I copied and pasted
bin/rails db:migrate RAILS_ENV=development
and this was what I got:
I recommend you to check the status of all your migrations, maybe there is another migration pending. Use this command rake db:migrate:status to see all migrations status.
You can also reset your db and do the migrations all over again.
rake db:reset (which runs db:drop then db:setup) then run your migrations rake db:migrate.
Also, please check if any of your migrations are referencing on any other migration that is still not created!
P.S. Please add the complete error you are getting, so I can have a better idea what the problem is!
At this point, I think either of this should be the problem.
You cloned the files that was built on Mac to run on windows or Vice versa.
There are some queries you cannot pass to the database. Pending the type of database, you are using with your Rails application.
Crosscheck if your database settings are correct.
That's it, Try this.
1. For rails, 5+ do rails db:rollback
Bundle install (just to see if everything works well)
Run bin/rails db:migrate RAILS_ENV=development. if it still doesn't work, Look through the error report on your terminal, you should be able to see the things that are resisting the "migrate" command.
start your server with rails s --port=PORT_NUMBER. (e.g rails --port=4000)
You have a problem with the db/migrations/20180619074210_create_users.rb file. Delete it as the migration already failed. You do not need to undo it (rollback).
Also, I would highly recommend using command line to create a migration file until you are familiar with them. And if you have time, checkout Rails guides for Active Record Migrations.
Example:
1) Create a user table with name column (string) and age column (integer):
rails generate migration CreateUsers name:string age:integer
2) This will create a similar file with a different timestamp. In my machine I got db/migrate/20180626151529_create_users.rb.
3) If you open that file, you can examine the changes:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.integer :age
end
end
end
4) If all looks good, you need to run this migration file. That can be achieved by:
rake db:migrate
Related
I am rather new to Rails, and I am attempting to run an app from my console. When I open up the site, however, I see this error message. Does anyone have any insight on what it means?
When I try running "rails db:migrate RAILS_ENV=development" I just get:
Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development
When I try running just "rails db:migrate" or "rake db:migrate" I get:
ArgumentError: wrong number of arguments (0 for 1)
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:600:in `migrate'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.1/lib/active_record/migration.rb:573:in `check_pending!'
Thank you so much!
To resolve this issue, run: bin/rails rake db:migrate RAILS_ENV=development
And the issue was raised due to pending migrations, which are created migration files at /db/migrate directory by doing add/remove fields to the existing active record or relation(table) in database.
Migrations are stored as files in the db/migrate directory, one for each migration class. The name of the file is of the form YYYYMMDDHHMMSS_create_products.rb, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The name of the migration class (CamelCased version) should match the latter part of the file name. For example 20080906120000_create_products.rb should define class CreateProducts and 20080906120001_add_details_to_products.rb should define AddDetailsToProducts. Rails uses this timestamp to determine which migration should be run and in what order, so if you're copying a migration from another application or generate a file yourself, be aware of its position in the order.
Example:
$bin/rails generate migration AddPartNumberToProducts
This will create an empty but appropriately named migration:
class AddPartNumberToProducts < ActiveRecord::Migration[5.0]
def change
end
end
If the migration name is of the form "AddXXXToYYY" or "RemoveXXXFromYYY" and is followed by a list of column names and types then a migration containing the appropriate add_column and remove_column statements will be created.
$ bin/rails generate migration AddPartNumberToProducts part_number:string
will generate
class AddPartNumberToProducts < ActiveRecord::Migration[5.0]
def change
add_column :products, :part_number, :string
end
end
Referred at: http://guides.rubyonrails.org/active_record_migrations.html
Goto your app folder and run below mention command:
bin/rails db:migrate RAILS_ENV=development
I ran commands
# wait until this completes successfully before continuing
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
and it solved the problem for me.
How to create more models in ruby on rails? at first create one model like this type of command:
$ rails g model user name:string email:string
Create properly, but when I try to create second model like this type of command:
$ rails g model job job1:string job2:string
Not create any model & showing this type of error
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
How can I solve this issue? I'm using Rails 4.2.5, ruby 2.1.7p400 & mysql
As the error you are getting Migrations are pending. To resolve this issue,...
To fix this you need to follow the below step.
1: In terminal you need to Run
rake db:migrate # in order to run the all pending migration task
2: Then
rails g model job job1:string job2:string
Should work!!!
hope this solve your issue!!!
Do a rake db:migrate in your terminal every time you generate a model(s) or make some changes in your model. Only then it will be applied to your rails app.
For more on migration you can visit: http://guides.rubyonrails.org/v3.2/migrations.html
Each time you create a migration or/and add a model (or any changes to your table) you have to run rake db:migrate to apply those changes.
You can also see which migrations are pending by using rake db:migrate:status
Hope it sheds a bit of light on your issue.
I'm currently following the ruby on rails tutorial: http://guides.rubyonrails.org/getting_started.html.
I am trying to save data into the database. However, when I run: rails server I get the following error:
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
I've looked at the other articles and when I run:
bin/rake db:migrate
I get a rake aborted!
After running:
rake db:abort_if_pending_migrations....
I see that:
You have 1 pending migration:
20150805200129 CreateDatabases
SQLite3::SQLException: table "databases" already exists:
and it just tells me to run rake db:migrate to start again.
It seems that it already exists. Is there a way to cancel the pending migration?
Sometimes, even dropping a local development database is not a good idea.
There are better ways to delete/destroy a specific migration in your Rails application.
You could use rails d migration command to destroy a particular migration:
rails d migration MigrationName
To undo the changes corresponding to a particular migration, you can use db:migrate:down method like this:
rake db:migrate:down VERSION=XXX
Sometimes, things could get more messy and in those situation another handy thing is to take a look at the schema_migrations table in your database which has all the migrations with their version saved in it.
You can delete a particular migration from this table like this:
delete from schema_migrations WHERE version = VERSION;
if you don't want that migration to be present anymore.
Your migration may have failed midway (so it created the table, but didn't finish).
You are just using development environment, so it's okay to just drop the database and rebuild it from scratch:
rake db:drop # THIS WILL DELETE YOUR DATABASE
rake db:create
rake db:migrate
If you are like me and maintain your database structure outside of Rails, you can just delete the migration file from db/migration. I got the error in the OP's question when I used the rails generate command to create a model class, forgetting that it also creates a migration file.
Do not use this method if you rely on Rails to maintain your database structure!
I keep my Rails structure file up to date by building it from the database using:
bundle exec rake db:structure:dump
I do not encourage to drop the database and start from the beginning especially when you already have the data inside the database.
My approach to this will be migrate first, then rollback. After that you can safely delete the migration file. So the procedure is as following.
rails db:migrate
rails db rollback
rm db/migrate/your_last_migration_file.rb
You can recreate database and run all migrations in your development environment with such command
rails db:migrate:reset
If you want to revert the wrong migrations, You can drop the whole db using this:
rake db:drop
Then remove the migrations file manually(This wont corrupt the db when you recreate as the Schema migrations would be dropped as well).
Then run
rake db:migrate
And if there is data to be seeded, then run this as well
rake db:setup
I am working on the onemonthrails course.
I need to execute a migration using the below rake command. The devise migration file looks to be setup correctly.
rake db:migrate
I am expecting to see some kind of confirmation like the following:
=======DeviseCreateUsers: Migrating=======
create table (:users)
-> 0.0145s
add_index (:users :index etc...
etc..
Instead, my terminal reverts straight back to the command line (with no errors) but nothing appears to have been done. For example:
Petes-Computer:example Pete$ rake db:migrate
Petes-Computer:example Pete$
The below error in my browser confirms it didn't work.
ActiveRecord::StatementInvalid in Devise::RegistrationsController#new
Could not find table 'users'
There are a couple of other posts on this but no luck resolving. I am very new to ruby/rails/rake; please could someone advise.
After our discussion, it turns out Pete's migration file didn't have an extension. rake db:migrate didn't pick up the migration because it wasn't an .rb file.
I had just installed devise so the table didn't have any data on it except one user (me).
I was re-doing the database all over again so I dropped it all. I did rails g scaffold to generate 6 new models and controllers and did rake db:migrate
In my /db/migrate directory I have the devise file with the filename 20130603211907_devise_create_users.rb
Here is the issue: If I do rake db:migrate:down VERSION=20130603211907 it will delete all the new migrations.
How do I run a migration again, without deleting all the newer migrations?
It will run the down and then the up step (This command can drop your table!):
rake db:migrate:redo VERSION=xxxxxxx
To prevent your table from being deleted you could do this in conjunction with commenting out the down step temporarily.
Thanks for the help everyone. This is what worked for me:
WARNING: these commands will delete all data in your database!
rake db:drop
rake db:create
rake db:migrate
rake db:migrate:up VERSION=20090408054532
this will migrate all file upto VERSION=20090408054532
checkout Run a single migration file
If you are developing locally and it wouldn't hurt to remove all data from your models you could simply drop your db and then create & migrate from scratch:
Purge or recreate a Ruby on Rails database
You can call rake db:migrate:redo VERSION=20130603211907 which will rerun the specified version.
Also, if you have migrations that should only run when going up the migration chain (e.g. data transformation, copying, etc.), you can specify a
def up
do_something
end
and def down (going down), def change (both ways) method respectively.
To temporarily disable migrations, just, for example, rename the method def up to def up_ and it will be ignored when processing the migration chain.