# 20221117 Rails ###### tags: `Ruby & Rails` [TOC] <政庭> ## rials console --sendbox * find(id) 找不到會噴訊息 (ActvieRecord::RecordNotFound) * find_by(key: value) 找不到噴nil * find(id)、find_by() 只會撈一筆資料 回傳都是單一元素 * find_by!(key: value) 找不到會噴訊息 * where(id: 2) 拿到的是陣列 一次找多筆資料 * where("id > 100") 找不到回傳空陣列[] ### 例外發生時的處理方法begin...rescue... ![](https://i.imgur.com/mrJBl79.png) #### select * from .... where id=2 ### 使用Controller 來控制錯誤訊息的出現 ```ruby class WishListController < ApplicationController rescue_from ActvieRecord::RecordNotFound, with: :not_found ``` 另外在檔案最下面 定義一個私有方法 ```ruby private def not_found render html: "找不到" end ``` 把原本的show_wish改回 ```ruby def show_wish @wish_list = WishList.find(params[:id]) render html: @wish_list.title end ``` ### 找不到頁面一般會出現404頁面 ```ruby private def record_not_found render file: "#{Rails.root}/public/404.html", status: 404, layout: false end ``` ## 把每個類別會使用到的方法寫進class ApplicationController * 把剛剛的404頁面,噴出錯誤訊息寫入class ApplicationController ```ruby class ApplicationController < ActionController::Base rescue_from ActiveRecord::RecordNotFound, with: :record_not_found private def record_not_found render file: "#{Rails.root}/public/404.html", status: 404, layout: false end end ``` ## 抽象類別 VS 具象類別 * 越上層越抽象 * 越下層越具象 ## simple_format() 在後端時處理在view頁面的文字換行 ```ruby <%= simple_format(@wish_list.description) %> ``` ## delete 會使用 post 方法 傳送資料 ### 超連結標籤並沒有 post 方法所以我們要幫他加工 ```ruby <a date-method="delete" herf="連結路徑">文字描述</a> ``` ### data_method="delete" 會產生一個表單並用 post 方法傳送資料 * (UJS)概念:Unobtrusive Javascript 非侵入式javascript ## Controller 整理 ### before_action :find_wish_list, only: [:edit, :update, :destroy, :show] * before_action 最好使用正面表述的only ## View 整理 * 使用partial render的方式 ## 餵廣告時不要讓廣告自己去拿實體變數 * 要給廣告吃"區域變數" ## form_with / form_for /form_tag 差別 ## 第2個 Model (User Model) ### Users (users) - nickname: string - email:string - password:string --- ### Profile(profiles) - address - phone - gender ### 資料庫正規化 - 第1~5正規化 --- <侑庭> ## 初階find() , find_by() ,where() 用法 find(ID) 找不到會噴ActiveRecord find_by (key: value) 找不到會給nil find_by!(key: value) 加驚嘆號就會變嚴謹,找不到會噴ActiveRecord where(key: value) 可以撈多筆資料,但是找到會回傳[] begin..rescue 以上所有 SQL語法會是這樣 select * from … where id=2 ActiveRecord >語法沒錯但使用這可能使用錯誤 ## 找不到頁面404 * 400/500 都會放在routew之前,在如果任何東西壞掉之後,就可以不用進去MVC的路徑,而在外面就能拿到了 ## application_controller 為什麼在我們的controller裡面要有他? 因為在上去一層就會到Rails的的頂層的文件而這些我們是不會 ## simple_format是什麼 * `simple_format` 可以幫文章自動換行,為什麼要用這個而不是用其他的呢?因為這是打在同一格匡裡面的文章,所以是一整包袋進去如果用br 或是其他會沒辦法換行所以可以使用ruby的方法 ```ruby <%= simple_format(@wish_list.description) %> ``` ## UJS = Unobtrusive JavaScript UJS 的意思是會掃描畫面上的東西,如果在a_link裡面有包裹data他就會去做掃瞄並用JS動態生成表格,把東西掃到標格內 ## 統一整理收割歸納 before_action :接方法名稱 , only: [:edit , :destory , :update , :show] before_action :接方法名稱 , except: [:edit , :destory , :update , :show] partial render 局部渲染,會同步去找底線的檔案`_form.html.erb`(概念是共用檔案),然後需要用的網頁就可以用`<%= render "form" , wish_list: @wish_list%> ` ## form_with vs form_for vs form_tag form_for(model) 一定要接一包東西但是就是不能是nil form_tag(url) form_with(Hash) 只要在hash內給他是 | Column 1 | Column 2 | Column 3 | | -------- | -------- | -------- | | Text | Text | Text | (modle: tim)他就變成 `form_for` ,如果胎後面 pluralize --- <于婷> ## .find/ find_by/ find_by!/ where | .find(id) | find_by(key: value) | find_by!(key: value) | where(key: value) | | --- | --------- | ------------------- | --------------------- | | 找不到會噴訊息 (ActvieRecord::RecordNotFound) | 找不到噴nil | 找不到會噴訊息 (ActvieRecord::RecordNotFound) | 找不到回傳空陣列[] | | 只會撈一筆資料 回傳都是單一元素| 只會撈一筆資料 回傳都是單一元素 | 只會撈一筆資料 回傳都是單一元素 | 拿到的是陣列 一次找多筆資料 | ## 將顯示的內文自動換行 ```ruby <%= simple_format(@wish_list.description) %> ``` ## UJS = Unobtrusive JavaScript 非侵入式JS 目的是為了不要在html內放JS語法。 ## form_with / form_for / form_tag | form_with(Hash) | form_for(model) | form_tag(url) | | -------- | -------- | -------- | | 會依據內容判斷是 form_for 或 form_tag ,但比form_for少了class和id屬性,可自行加| 不能是nil | 網址 |