製作BLOG MEMO

這是在嘗試用rails寫blog時的memo

製作月曆table的時候已知用colspan

try真的是很方便的功能啊,不用一直在那邊判斷是不是nil


modal+raty一起使用的時候不知為何modal加了fade之後會讓raty失效. 並且要把raty的code放在 $('.modal').on('shown.bs.modal', function(){}之下,如果還是不會動的話試看看設setTimeout()


因為heroku是用git來上傳,但是有些檔案比方說application.yml,database.yml不適合被傳上去github,所以建了一個production branch只用來push heroku git push heroku production:master


dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
  Referenced from: /usr/local/bin/psql
  Reason: image not found
[1]    31890 trace trap  psql

最後是brew reinstall postgresql解決這問題


執行rails c出現

/usr/local/Cellar/ruby/2.3.1/lib/ruby/2.3.0/irb/completion.rb:10:in `require': dlopen(/usr/local/Cellar/ruby/2.3.1/lib/ruby/2.3.0/x86_64-darwin15/readline.bundle, 9): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib (LoadError)
  Referenced from: /usr/local/Cellar/ruby/2.3.1/lib/ruby/2.3.0/x86_64-darwin15/readline.bundle
  Reason: image not found - /usr/local/Cellar/ruby/2.3.1/lib/ruby/2.3.0/x86_64-darwin15/readline.bundle

最後是重跑一次

rvm cleanup all
rvm autolibs enable
rvm install 2.3.1

http://stackoverflow.com/questions/16756287/cannot-execute-rails-console


打rails s卻跳error

`to_specs': Could not find 'railties'

解決方式:

  1. gem install rails
  2. bundle

從view傳值到controller

<%= link_to 'Blog留言管理', admin_comments_path(type: "blog") %>

這樣就可以從params[:type]取得值


回到上一頁 rails 5開始可以使用redirect_back

http://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_back

redirect_back(fallback_location: root_path, notice: '刪除成功')

如果可以就回到上一頁,不行的話就回到指定的路徑


N+1 query 一開始碰到的都是比較簡單的狀況 比方說改成

Post.includes(:comments).find(params[:id])

就可以解決,碰到需要一次includes兩個的時候卡住了一下

Post.includes([{comments: :commentable}, {comments: :replies}]).find(params[:id])

上面那樣我就可以一次包括comments相關聯的兩個table了


Heroku的log有時候沒辦法看出錯誤訊息,執行heroku run rails console可能可以看到更多資訊


rake assets:precompile RAILS_ENV=production

在production的時候要記得執行

開發的時候asset都是直接用原始檔,你有幾個css幾個javascript...之類的你看html的原始碼就會有幾個,但是production的時候必須要用public下壓縮後的檔案,這樣效能才會比較好 如果沒先執行的話,assets的東西會出不來

results matching ""

    No results matching ""