2015年3月16日 星期一

rails_0311

七步驟:建立新專案 -> 啟動伺服器 -> 路徑設定 -> controller建立 -> (model建立) -> view(畫面)建立 -> 網頁開啟




01.建立新專案
   $ rails new my_library
   切換到my_library下
   $ cd my_library/

02.啟動伺服器
   $ rails s -p $PORT -b $IP


03.網址對應表
   /config/routes.rb
   當其中的對應不見,不管網頁是否存在,都會連不到網頁。

   root 'static_pages#index'
   當輸入網址後,若沒特別指定,就會去找static_pages的controller當中的index方法。

   get '/about', to: 'static_pages#address'
   當指定到about時,就會去找static_pages中的address方法。

   resources :books
   自動生出8個url網址所對應的controller和方法。


 - 能看到routes中所有的url網址所對應的controller和方法
   $ rake routes


user -1> routes -2> controller <4-3> moudel
 7↑                             |     5↓↑6    
   ------------------------   view    


04.要指定到controller的static_pages,需先建立controller
   $ rails g controller static_pages
   在controller建立好的同時,views下也會產生一個相應的資料夾。

   在剛建立/app/controller/static_pages_controller.rb中,加上index方法
   def index
     @books=['ruby', 'happy', 'test']
   end

(05.)新增一個model,叫Book(這步驟是後面那個檔的,某些情況下可以不用)
   $rails g model Book
   在model建立完成時,/db/migrate下也會出現一個新增資料表的ruby檔


 - 產生新的migrate
   $ rails g migration change_title_name
 - 改欄位屬性
   change_column :books, :title, :text
 - 改欄位名稱
   rename_column :books, :name, :title
 - 更改完要存回資料庫
   $ rake db:migrate
 - 看資料庫更新的狀況
   $ rake db:migrate:status
 - 回到上一步(不建議使用,有可能刪掉資料庫)
   $ rake db:roolback

06.畫面的建立
   在/app/views/static_pages/index.html.rb中,加上
   <h1>All Books</h1>
   <ul>
   <% @books.each do |book| %>
     <li><%= book %></li>
   <% end %>
   </ul>


<% %>:表示要翻譯的ruby程式


07.開啟網頁


  --------------- 分隔線 ---------------

重複04、05、06步驟
04.
 - 新增一個controller,叫book
   $ rails g controller books

   /app/controllers/books_controller.rb
   class BooksController < ApplicationController
     def index
       @books = Book.all  #列出所有books
     end
     def new
       @book = Book.new  #新增一個空的book
     end
     def create
       @book = Book.new(book_params)  #真正的新增book
       if @book.save
         redirect_to books_path  #轉址到book的首頁
       else
         render :new  #取用new用到的view
       end
     end
     private
       def book_params
         params.require(:book).permit(:title, :description, :auther, :publish_date)
       end
   end

06.
/app/views/books/index.html.erb
<h1>Books</h1>
<ul>
    <% @books.each do |book| %>
    <li><%= book.title %></li>
    <% end %>
</ul>

<a href="/books/new">new1</a>
<a href="<%= new_book_path %>">new2</a>
<%= link_to "new3" ,new_book_path %>
<%= link_to "new4" ,"books/new" %>


/app/views/books/new.html.erb
<%= form_for(@book) do |f| %>
<%= f.label :title, "書名" %>
<%= f.text_field :title %>
<br />
<%= f.label :description, "說明" %>
<%= f.text_area :description %>
<br />
<%= f.label :auther, "作者" %>
<%= f.text_field :auther %>
<br />
<%= f.label :publish_date, "出版日期" %>
<%= f.date_field :publish_date %>
<br />
<%= f.label :is_online, "上架" %>
<%= f.radio_button  :is_online, "true" %>是
<%= f.radio_button  :is_online, "false" %>否
<br />
<%= f.label :is_online, "上架" %>
<%= f.check_box :is_online %>
<br />
<%= f.submit %>
<% end %>
<%= link_to "back", books_path %>


<%= %>:執行程式並把結果輸出
<% %>:純執行,不輸出結果

沒有留言:

張貼留言