Start the server:
Open a Rails console:
Create a controller/action/view/route all in one step:
rails generate controller controller_name action_name
Set up a new route (in
"/my/route" => "controller_name#action_name"
Set up a new route with a parameter (in
"/item/:item_id" => "controller_name#action_name"
Access a route parameter, GET query argument, or POST data in a controller or view:
Insert some Ruby code into a view:
3 plus 4 equals <%= 3+4 %>
Create a model and its migration:
rails generate model Product name:string description:text
Run the migrations locally (you have to do this every time you add a migration):
Run the migrations on Heroku (you have to do this every time you add a migration):
heroku run rake db:migrate
db/schema.rbtells you what the current shape of your database is. This useful to look at during development!
Use simple, singular names for your models in camel case, like
Use GET for routes that should be crawl-able by a search engine.
Use POST for routes that mutate the state of the database, or for logging in.
For Project 1.2, you will have to deal with the same-origin policy.
Models in Rails correspond to tables in the database.
The model is named
LikeThis. The corresponding table is named
Thisthat look like
:thisare called "symbols." Python does not have this. You can think of them like immutable, hashable, singleton strings.
If you are using Ruby/Rails on your personal machine and not in a VM, use RVM to install Ruby.
Make sure you are using a Rails version >= 3.1.
You need both
Gemfile.lockin your repository.
If you're having Gemfile issues on Heroku, run
bundle installand try again.
In views, note the difference between
<%= 3+4 %> and
<% 3+4 %>. The first one evaluates the expression
3+4, casts the result to a string, and inserts it into the page. The second one merely executes the code and throws away the result. Use the first one for inserting server-generated data into a page; use the second one for control flow constructs like if statements and looping.
Heroku does not support SQLite, the default Rails database. To push to Heroku, delete the line
gem "sqlite3" and put this in your
Gemfile (this will use SQLite locally and PostgreSQL on Heroku):
group :production do gem 'pg' end group :development, :test do gem 'sqlite3' end
If you have problems installing the
pg gem on Ubuntu, run
sudo apt-get install libpq-dev and try again.
One last tip: from a view, you may find yourself wanting to insert some HTML into the
</head> tags, such as the page title, or extra JS/CSS includes. To do this:
app/views/layouts/application.html(this is the default layout for all your pages).
<%= yield :head %>somewhere inside the
- In the view, you can now do something like:
<% content_for :head do %> this_will get_inserted_into_the_head_tag <% end %>
If you use this for setting the page title, be careful not to set the title twice (once in the layout and once in the view).
You may get the error "uninitialized constant Rake::DSL" when you run:
The problem is that Rake version 0.9.- breaks Rails. Solution: edit the Gemfile to not use this version of Rake. Either works:
gem "rake", "0.8.7"
gem "rake", "!=0.9.0"
bundle update rake