I am currently using Rubymine for ruby on rails development.
After enjoying the console for testing purposes, I want to use the console instead of the build-in Rails Generator for creating new models and such.
But I can't figure out how to use the rails console (Tools | Run Rails Console) for generating -lets say- a new model "user".
rails generate model user name:string
gives me
NameError: undefined local variable or method `string' for main:Object
I think I'm just missing something very basic here but I couldn't come up with a solution by myself after several tries :/
I hope you can help me out.
Greets
If you feel that you really must use the console to do this, you have a few options to get to the system.
You can use backticks.
or
You can use system()
Your command would then look like system("rails generate model user name:string") or it would look like `rails generate model user name:string`
The command you are trying to run is not something that should be run from a ruby console. It is a bash command. The error you are getting is because you are trying to run something that is not ruby in a ruby environment.
Related
I am getting error in irb
NameError: uninitialized constant Student
for Student.new or whatever model operations are there.
But in rails it gives no error and it works fine. What would be the reason?
This error only happens in Windows, same code I have in Linux and there it works fine.
What makes the difference here?
irb has nothing to do with your rails project.
What you want instead is to run
rails console
from within your rails project directory. Here you have access to everything defined within the application - Rails loads everything automatically.
rails c
Create new tab in terminal. Here you may work with modals. Like CRUD operations.
But make sure your terminal must pointing in rails working directory before trying rails c command ...
I used to be able to type rails generate or rails generate model to
get help information for different rails command lines, for some
unknown reason, now if I type rails generate or rails generate model,
it always show me the help message for rails new, what is going on?
This normally happens if you are not in a rails path, so make sure your curfrent terminal / cmd is in a directory with a rails app you can change dirfectory with cd "/putdirectoryhere"
Mac OS X 10.7.3 Lion,
Ruby 1.9.2,
Rails 3.2.2,
Sass 3.2.3
Following this tutorial:
http://activeadmin.info/documentation.html
Following this video tutorial
http://www.youtube.com/watch?v=tAxlrHcEg9U
I add the activeadmin gem, run bundle install, then run
rails generate active_admin:install
rails generate active_admin:resource POST
Only after creating the app/admin/posts.rb and trying to run either
db migrate
rails server
fails with the error
uninitialized constant Post NameError
with out that posts.rb file i am able to run the admin interface error free.
I tried moving the sass-rails gem out side of the :assets in my gem file and re-running bundle install as suggested in another question, but to no avail I still have the error
according to the getting started active admin tutorial "Post" is suppose to be a module name so i assume the code above is calling a class method (ActiveAdmin as the class, register as the method) and sending the module as a parameter and the block do end
Regardless the error is implying that RoR doesn't know what Post is. As if it does not exist. Being new to rails i do not know how to navigate well, meaning i do not even know where this ActiveAdmin source file is in order to dig through it for a method Post
Thank you for the consideration and your time, I appreciate it.
The linked tutorial assumes that you have already created a model named Post (and have run rake db:migrate to link it to the database). The purpose of the rails generate active_admin:resource Post command is to tell ActiveAdmin that you want it to consider the Post model in part of what it does.
Historically, you'll see models like Post and User in Rails a lot -- these are the commonly used examples of creating a blogging application (a user can create blog posts).
So, whatever models you have in your application can be registered with ActiveAdmin by replacing Post with the name of your model.
Another note: while generators like this tend to be forgiving, a Post is a model that is defined in post.rb and is linked to a SQL table called posts. Be careful with things like upper- and lower-case, and singular and plurals. In Rails they all fit together in a special way.
I typed this into terminal:
rails generate migration CreateAddress
and instead of creating a new migration file, it created the entirety of a naked rails app.
What is wrong here?
The generate script is a Ruby script, so you should just call it with ruby.
Also, you usually want to call that script from the top level of your app, so:
$ ruby script/generate migration CreateAddress
The reason you have your issue is because executing rails simply creates a naked Rails app in your current directory with the first argument as its name. In this case, that's obviously "generate".
I have a RoR project on my Windows 7 PC.
I want to create some Ruby code that I can execute from the cmd.exe command line that manipulates the development database (via database.yml) of the project. (I don't want to have to run my utility code via a web page.)
What is the best way to go about pulling this off? (I'm a newbie.)
I can't put the code in the test/ directory because that executes against the test database.
I tried just creating a utility.rb file under app/ but when I run it I get this:
utility.rb:5: uninitialized constant ActiveRecord (NameError)
My standalone file obviously doesn't know about the rest of the rails framework.
Any suggestions?
Rails comes with a utility to do exactly this. Instead of using ruby filename, use script/runner filename (from within the top-level directory for the Rails project), which will automatically load up your Rails environment before running the script.
However, if what you're trying to do is manipulate the database, the right answer is probably to create a migration. Most people assume that migrations are only for changing the structure of your database (adding or removing columns or tables) but they can also be a great way to add seed data or manipulate all the data in the database.
You can write your own rake task which depends on :environment and pass RAILS_ENV=development when executing it.
Nice screencast about it: screencast