I am doing the following tutorial, to learn how to use dhtml in rails.
http://www.dhtmlx.com/blog/?p=426
I reach the point where I create the migration however when I do the rake db:migrate I recieve the following error:
An error has occurred, this and all later migrations canceled:
uninitialized constant CreateUsers::User
Don't seem to understand how I can possibly be recieving this error especially by simply following a tutorial. "/
Make sure you generate the User model before running the migration. (ruby script/generate model user in the tutorial, but rails g model User for Rails 3)
EDIT: As Jeremy pointed out, generating the model will create a migration file for you. The tutorial shows things in a somewhat backwards order.
Related
I'm getting this error message when I run the above code generator: (I'm just a beginner)
invoke active_record
Another migration is already named create_posts.....
Use --force to remove the old migration file
What do I type into the terminal window to "use force"
You are getting the following error because you already have a migration named create_posts in your rails application.
invoke active_record Another migration is already named create_posts..... Use --force to remove the old migration file
So, what you need here is first remove the existing migration and then generate the scaffold.
rails d migration create_posts
rails generate scaffold post title:string body:text
Or
You could generate the scaffold using --force option
rails generate scaffold post title:string body:text --force
EDIT
As per your comment:
I did that and then a whole bunch of code appears with the lines of
code sating invoke...exist...identical.
It means that you already ran a scaffold once for Post successfully and you are trying to generate the scaffold again.
I am not sure why you are doing that BUT identical is not an error. Its just that Rails is telling you that you already have a particular file so I am not creating again.
You can reset your database if you don't care about losing your database with this :
b rake db:reset
This will re-reun all your migrations. But take note! Your migrations should be able to run from one end to the other. So if something is "not working" with the regular rake db:migrate, then you should resolve that issue is specifically.
Show me a more descriptive error, and I can tell you more.
You should add other migration in order to change your Post table as you want it to be.
Your could begin with rails g migration and see the help provided.
If you want to get away with it you can delete the migration that created the Post table (but I guess you would need to delete the DB)
After the first time you generate a scaffold, by default Rails will not overwrite the existing scaffold. This is to ensure that you don't accidentally destroy a lot of work.
If you're really sure you want to regenerate the scaffold and delete any changes you might have made to any of the generated files, try:
rails generate scaffold post title:string body:text --force
I'm relatively new to Rails and I'm running into an issue while trying to set up Devise. I believe the issue stems from the fact that I already generated a user scaffold before trying to install Devise, yet I don't know how to solve this problem. When I proceed through the Devise setup, I get to the step where I have to enter the following code:
rails generate devise User
That works, and I get this back from terminal:
invoke active_record
create db/migrate/20120609032448_add_devise_to_users.rb
insert app/models/user.rb
route devise_for :users
The next step is to migrate the database, which I attempt but get the following error:
== AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar(255) DEFAULT '' NOT NULL
Tasks: TOP => db:migrate
I've tried destroying the original User scaffold along with the original User migration but I keep getting stuck at this error. Help would be much appreciated!
Your new migration probably has an email column defined in it. Comment that line regarding adding an email column out and run it again. It's likely you've already got an email column in your model.
Try going to this file
db/migrate/20120609032448_add_devise_to_users.rb
and in the code where it says
change_table(:users)...
alter this to
create_table(:users)...
I am new to Ruby on Rails . This might be a very foolish question.
I have created a migration using
rails generate migration Kapol name:string position:integer
rake db:migrate
Then using phpmyadmin i copied the database already present
I then opened up rails console
My question is can i use the method Kapol.find(1)??
because when i tried it using singular or plural it says
unitialized constant:Kapol
I know that there has to be a method but where to specify it?
You must generate a model in case to create a table for it, because the migration is usually used to modify existing tables.
It might be confusing that the model generator also creates migration file in your migrations folder. The only difference is that the model generator also generates initial code to create table, on the other hand, the migration generator creates only migration file without initial code.
rails generate model Kapol name:string position:integer
More information: http://guides.rubyonrails.org/getting_started.html#generating-a-model
If you're very new to Ruby on Rails, probably the best thing for you to do is create a scaffold, which gives you your migration file, your model file, your controller file, and various view files, test files, etc. etc., which all work well together. Then you can play with these and build up from there.
rails generate scaffold Kapol name:string position:integer
If you're happy with the migration that was automatically generated, then rake db:migrate and you're all set.
As Andrew says below, you can also just generate any of those files one at a time by replacing 'scaffold' with 'model', etc.
Your Kapol.find(1) is correct.
HI,
I got this error when i am testing my rails application. I dont have the table named 'instructions'. But it shows a error like "ERROR: relation "instructions" does not exist". Totally, I got same error for 64 tests as 64 errors.
I am using rails 3.0, Ruby 1.9.2, Netbeans 6.8.
PS: I didnt creat Instruction manual for rails application.
Error:
test_should_get_index(HomeControllerTest):
ActiveRecord::StatementInvalid: PGError: ERROR: relation "instructions" does not exist
LINE 1: DELETE FROM "instructions"
^
: DELETE FROM "instructions"
Kindly help me in this regard
You may want to run rake db:test:prepare , the table instructions isn't present in your test database.
Check your fixtures. I hit an error like this when using Rails scaffolding to generate a subclassed resource.
The generator had created files in my test/fixtures/ directory that didn't correspond to any actual tables in my database (since the model was using STI under another table).
When running tests, Rails attempted to instantiate all my fixtures, and an error similar to yours was generated when it could not find a table matching the name of the fixture.
Deleting the unneeded fixture file cleared my error and got tests running.
Hope that helps you, or others who arrive here searching that error, as I did. :]
NameError in GenresController#index
uninitialized constant GenresController
RAILS_ROOT: C:/Users/Will/Desktop/INSTAN~1/rails_apps/talewiki
I've created a table called Genres and when I try to connect to it via local host I get the above error.
Any ideas?
With all the questions you're asking I believe you're an absolute beginner regarding ROR. Perhaps you should visit some tutorials to learn rails.
I don't know what your genre model describes, but I think it will have a name.
Basic steps for a basic genre model:
Delete the table for your genres if created manually (with SQL code)
DROP TABLE genres;
generate a complete scaffolding for genres:
$ ruby script/generate genre name:string
$ rake db:migrate
Now you have a complete controller for all CRUD actions for a simple genre model
If I were you I would read some tutorial about RoR, because you make the impression that you don't understand RoR or the MVC principle behind it. A good start would be: http://storecrowd.com/blog/top-50-ruby-on-rails-tutorials/
You need to generate a controller to handle the index action when your browse your application on localhost
ruby script/generate controller genres index
run that from your console within your application and it will generate the GenresController with the action index (it will be an empty action but you shouldn't see an error when browsing http://localhost:3000/genres/)
file
C:/Users/Will/Desktop/INSTAN~1/rails_apps/talewiki/app/controllers/genres_controller.rb
must be present