###### tags: `Ruby` <style> img{ border: 5px solid #aaaa; width: 100%; } </style> [TOC] # 4/20 # 製作留言區   ## route  ## controller  ## view ## commentcontroller   按下create會到這個頁面並顯示ok ### 幫留言加驗證  ### 建立關聯   ### 篩選參數   因為和item、user有關連所以需要傳入這兩個的id但current_user這時是email  改為id  application.controlle裡因為session已經改成id那該如何拿到user的資料?用User.find(id)所以每次用current_user就是再撈資料庫  用find找不到session時會發生錯誤所以用find_by找不到會給nil    ### 測試   ### 關聯用法  owner has_many stores就會有4個方法  就可以直接新增商店  ### 給@comment參數 comment有item和user方法因為它屬於他們   方法一  方法二、三  ### 用render保留資料 ### 顯示留言   使留言排序新的在最上面  #### 方法二  ### 套bootstrap    ### N+1問題  解決方法  Eager loading ### save後不處裡會自動view裡的同名檔案(可以是js檔)  ### 把留言用render的方式呈現   去找comments裡的comment.html.erb,comment:comment代表給區域變數 把原來的內容丟到erb裡面 ### js操控  這樣寫似乎js看不懂所以  但let並不能使用2次會錯誤代表不能送2次comment  所以改var  #### 送出完要消除內容  找到他的id  #### 新增愛心 ##### 下載加引入fontawesome      讓他可以按  ##### 按下後會轉址    script在application裡引入的 ##### 使用stimulus  自動新增   這樣寫會自動找到hello_controller  data-action="hello#heart會到hello_controller找到heart action  就可以避免按下愛心轉址  覺得hello不適合可以改名字,controller也要改喔  設定target   ## 時間複雜度 O(N) 搜尋時間隨筆數增加 建立索引可以增快速度 ## N+1 Query 解決  一次把所有email、created_at一次撈出來   views/comments下新增comment.html.erb把留言區放在這 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up