$ rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config) $ rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases) $ rake db:fixtures:load # Load fixtures into the current environment's database $ rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false) $ rake db:migrate:status # Display status of migrations $ rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n) $ rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR $ rake db:schema:load # Load a schema.rb file into the database $ rake db:seed # Load the seed data from db/seeds.rb $ rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first) $ rake db:structure:dump # Dump the database structure to db/structure.sql $ rake db:version # Retrieves the current schema version number
$ rails generate scaffold User name:string bio:text birthday:date $ rails destroy scaffold User $ rails g scaffold Post name --skip-assets $ rails db $ rails console $ rails g controller posts # 複數 $ rails g controller posts index new create # 後面可以接 action $ rails g model Post title body:text $ rails d model post
form_for 的用途是建立一個 html 的 form 表單,讓使用者可以透過 form 把資料傳回後端,新增或者更新某個指定物件的屬性。 這個方法(method)有幾種稍微不同的用法,取決於您想從 Rails 的 Model 中自動對應多少東西,或者說自動處理掉 Model 對應的部分 針對一般情況的 Model,我們透過傳入 form_for 一個字串或 symbol 來表示我們要對應的物件
# 當我們使用 RESTful 路由時內部的各種狀況範例如下: # # # 呼叫 post_url(post) # polymorphic_url(post) # => "http://example.com/posts/1" # polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1" # polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1" # polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1" # polymorphic_url(Comment) # => "http://example.com/comments" # # # ==== 其他範例 # # # an Article record # polymorphic_url(record) # same as article_url(record) # # # a Comment record # polymorphic_url(record) # same as comment_url(record) # # # it recognizes new records and maps to the collection # record = Comment.new # polymorphic_url(record) # same as comments_url() # # # the class of a record will also map to the collection # polymorphic_url(Comment) # same as comments_url()