Rails Study
Rails Philosophy
- DRY – “Don’t Repeat Yourself” – suggests that writing the same code over and over again is a bad thing.
- Convention Over Configuration – means that Rails makes assumptions about what you want to do and how you’re going to do it, rather than requiring you to specify every little thing through endless configuration files.
- REST is the best pattern for web applications – organizing your application around resources and standard HTTP verbs is the fastest way to go.
- Using resource identifiers such as URLs to represent resources.
- Transferring representations of the state of that resource between system components.
Install Rails
- according to steps on offical website do
1
gem install rails; rails new rails-test; cd rails-test; rails server
- when running
rails server
i met such problem:1 2 3
$ rails server /home/zheng/.rvm/gems/ruby-2.0.0-p0/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
This means i need a js engine, and i install the nodejs
instead, and it's ok
try to open
localhost:3000
, and you'll see welcome pageSwitch to MySQL instead of SQLite3:
- the config file is in config/database.yml and change the "development" section like that
- add
gem 'mysql2'
to Gemfile - run
rake db:create
to check is there any problem1 2 3 4 5 6 7 8
development: adapter: mysql2 encoding: utf8 database: rails-test pool: 5 username: root password: socket: /var/run/mysqld/mysqld.sock
Remember the test section will also be set to mySQL
Hello Rails
- Try to run
rails generate controller home index
then u will see many files created- create home folder and create index page
- modify
app/views/home/index.html.erb
which will tell us how to show index page - rm default index "public/index.html" which is the static page
- modify
config/routes.rb
uncomment and change toroot :to => "home#index"
Creating a Resource
Create by scaffold like
rails generate scaffold Post name:string title:string content:text
Running a Migration
- Migrations are Ruby classes taht are designed to make it simple to create and modify database tables. it's path
db/migrate/#timestamp#_create_posts.
- run the migration:
1
$ rake db:migrate
- Migrations are Ruby classes taht are designed to make it simple to create and modify database tables. it's path
Add a Link add
<%=link_to "My Blog", posts_path%>
to index.html.erbAdd some data, now using
rails console
1 2
p = Post.new(:title => "Title", :content=>"Hellooo", :name=>"myself") p.save #if error, try to show "p.errors.full_messages"
- Listing All Posts
- try to look at
app/controllers/posts_controller.rb
each method is resposed to each action - in
app/views/posts/
, each file corresponds to the action name, and u can modify the erb file1 2 3 4 5 6 7 8 9 10 11
class PostsController < ApplicationController # GET /posts def index @posts = Post.all # => an array of all posts respond_to do |format| format.html # index.html.erb format.json { render json: @posts } end end ... end
- try to look at
6.Customize Layout, u should create erb file in layout to specific layout, like app/views/layouts/posts.html.erb
, and the file application.html.erb
is for whole website