Hungkai's blog

On the way to be a programmer


  • 首頁

  • 標籤

  • 分類

  • 歸檔

  • 關於

  • 搜尋

Rails 101 push to Heroku failed - Precompiling assets failed

發表於 2018-08-03 | 分類於 Rails 101 |

Precompiling assets failed

還找不到解答,卡關中…

8/3 20:30 Edit: Devise.secret_key was not set. Please add the following to your Devise initializer

加上要求的設定值 config.secret_key 之後成功 git push heroku ch08:master,然後 db:migrate 又卡關了 Orz

8/3 21:00 Edit:
Gemfile

1
2
gem 'pg'
gem 'rails_12factor', group: :production

config/database.yml

1
2
3
production:
adapter: postgresql
encoding: unicode

進 Heroku 砍掉原本的apps,重新 push 再下 heroku rails db:migrate 終於開站了~

https://infinite-stream-15278.herokuapp.com/

閱讀全文 »

Rails 101 (Rails 5版) 整理post程式碼

發表於 2018-08-03 | 分類於 Rails 101 |

Rails 101 (Rails 5版) 整理post程式碼

使用 before_action 修掉重複程式碼

修改 app/controllers/posts_controller.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
+ before_action :find_group, only: [:new, :create]
+ before_action :find_post, only: [:edit, :update, :destroy]

def new
- @group = Group.find(params[:group_id])
@post = Post.new
end

def create
- @group = Group.find(params[:group_id])
@post = Post.new(post_params)
@post.group = @group
@post.user = current_user

def edit
- @group = Group.find(params[:group_id])
- @post = Post.find(params[:id])
end

def update
- @group = Group.find(params[:group_id])
- @post = Post.find(params[:id])
...

def destroy
- @group = Group.find(params[:group_id])
- @post = Post.find(params[:id])
@post.destroy
redirect_to account_posts_path, alert: "Post deleted"
end

private

+ def find_group
+ @group = Group.find(params[:group_id])
+ end
+
+ def find_post
+ @group = Group.find(params[:group_id])
+ @post = Post.find(params[:id])
+ end

發文時驗證使用者是否為群組成員

修改 app/controllers/posts_controller.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
  before_action :find_group, only: [:new, :create]
before_action :find_post, only: [:edit, :update, :destroy]
+ before_action :member_required, only: [:new, :create]

private

+ def member_required
+ @group = Group.find(params[:group_id])
+ if !current_user.is_member_of?(@group)
+ redirect_to group_path(@group)
+ flash[:warning] = "你不是這個討論版的成員,不能發文!"
+ end
+ end

成果

我發表過的文章分頁功能 & 新文章排序在前

閱讀全文 »

Rails 101 (Rails 5版) 刪除群組時一併刪除群組內的文章

發表於 2018-08-03 | 分類於 Rails 101 |

Rails 101 (Rails 5版) 刪除群組時一併刪除群組內的文章

bug 解決 => 當 group 刪除時,所屬的 posts 也要跟著刪除

當我們把 group 刪除掉的時候,
若所屬的 post 資料還在,就會變成所謂的孤兒資料

這會讓我們在打開 account/posts 頁面的時候跑出 error => 找不到該 post 所屬的 group (因為被刪了)

所以我們要設定當 group 刪除的時候,他所屬的 posts 也要跟著刪除

修改 app/models/group.rb

1
2
3
4
...
- has_many :posts
+ has_many :posts, dependent: :destroy
...

Reference

[ 2.0 ] 6 - 0. 實做簡單的後台機制

閱讀全文 »

Rails 101 (Rails 5版) 修改發表過的文章

發表於 2018-08-03 | 分類於 Rails 101 |

Rails 101 (Rails 5版) 修改已發表的文章

修改 app/controllers/posts_controller.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-  before_action :authenticate_user!, only: [:new, :create, :destroy]
+ before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]

+ def edit
+ @group = Group.find(params[:group_id])
+ @post = Post.find(params[:id])
+ end
+
+ def update
+ @post = Post.find(params[:id])
+ if @post.update(post_params)
+ redirect_to account_posts_path, notice: "Update Success"
+ else
+ render :edit
+ end
+ end

新增 app/views/posts/edit.html.erb

1
2
3
4
5
6
7
8
9
10
11
<h2 class="text-center">編輯文章</h2>
<div class="col-md-4 col-md-offset-4">
<%= simple_form_for [@group,@post] do |f| %>
<div class="form-group">
<%= f.input :content, input_html: { class: "form-control"} %>
</div>
<div class="form-actions">
<%= f.submit "Submit", disable_with: "Submiting...", class: "btn btn-primary"%>
</div>
<% end %>
</div>

成果

Rails 101 (Rails 5版) 額外作業

閱讀全文 »

Rails 101 (Rails 5版) 刪除發表過的文章

發表於 2018-08-03 | 分類於 Rails 101 |

Rails 101 (Rails 5版) 刪除已發表的文章

修改 app/controllers/posts_controller.rb

1
2
3
4
5
6
7
8
-  before_action :authenticate_user!, only: [:new, :create]
+ before_action :authenticate_user!, only: [:new, :create, :destroy]

+ def destroy
+ @post = Post.find(params[:id])
+ @post.destroy
+ redirect_to account_posts_path, alert: "Post deleted"
+ end

成果

Reference

Rails 101 (Rails 5版) 額外作業

閱讀全文 »

Sublime Text 3 settings for Rails developer

發表於 2018-07-31 | 分類於 Sublime Text 3 |

Sublime Text 3 settings for Rails developer

純粹記錄一下自己使用的設定,以免哪天需要重裝⋯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"ignored_packages":
[
"Vintage"
],
"color_scheme": "Packages/Material Theme/schemes/Material-Theme.tmTheme",
"theme": "Material-Theme.sublime-theme",
"font_size": 14,
"highlight_line": true,
"tab_size": 2,
"translate_tabs_to_spaces": true,
"trim_trailing_white_space_on_save": true,
"ensure_newline_at_eof_on_save": true,
"show_full_path": true
}
閱讀全文 »

Disable iTunes on macOS High Sierra

發表於 2018-07-24 | 分類於 iTunes |

Disable iTunes on macOS High Sierra 如何停用 iTunes

Why do I want to disable iTunes?

I don’t have an iPhone/iPad. And I listen to the music on Youtube. The most annoying thing is that iTunes often pops-up and I do not want it to interrupt my working. I searched on Google and realized lots people encounter the same problem due to using earphone on Macbook. So I decide to disable my iTunes.

Step 1: Disable SIP on High Sierra

  1. Power off your device
  2. Power on your device while maintaining ⌘ and R pressed
  3. Once you reach the recovery system, select Terminal from the Utilities menu
  4. Type $ csrutil disable and then reboot

If you want to enable SIP, type $ csrutil enable.

Step 2: Removing the “executable” property on iTunes.app

1
$ sudo chmod -x /Applications/iTunes.app

If you want to enable iTunes, run the following:

1
$ sudo chmod +x /Applications/iTunes.app
閱讀全文 »

iTerm2 搭配 zsh,換掉macOS原生Terminal 筆記

發表於 2018-07-23 | 更新於 2018-07-24 |

iTerm2 搭配 zsh,換掉macOS原生Terminal 筆記

http://www.iterm2.com/ 下載 iTerm,下載後拖到 “應用程式” 開啟

改變 shell 為 zsh

Mac 預設的 shell 叫做 bash。而 zsh 多了一些比 bash 人性化功能,能客製化的選項也比较多。
在終端機輸入$ chsh -s /bin/zsh即改變為 zsh

安裝 oh-my-zsh

1
$ curl -L http://install.ohmyz.sh | sh

修改 Theme

使用文字編輯器打開 .zshrc
找到 ZSH_THEME="robbyrussell" 行並修改如下

1
2
#ZSH_THEME="robbyrussell" <-- 將這行註解掉
ZSH_THEME="agnoster"

安裝 agnoster 所需字型

閱讀全文 »

Ruby on Rails installfest macOS 環境建置 安裝筆記

發表於 2018-07-23 | 分類於 Ruby on Rails |

Ruby on Rails installfest macOS 環境建置 安裝筆記

系統平台:macOS High Sierra 10.13.6 on MacBook Pro (Retina, 13-inch, Early 2015)

安裝 Xcode

到 appstore 搜尋並安裝 Xcode,經過漫長的等待安裝完,打開Xcode 首次執行.
Xcode 會要求同意「使用者協議」,選擇 Agree,然後就可以關掉了。

安裝 Homebrew

開啟終端機 (Terminal) 複製此行指令安裝,以下需安裝的都透過終端機執行

1
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

複製指令按Enter並輸入使用者登入密碼,此處會連 Xcode Command Line Tools 一併裝好

裝好之後下兩個指令確定以上兩樣工具都安裝完成
$ brew -v 出現 Homebrew 1.7.0 (版本號可能不同).
$ xcode-select -p出現"/Library/Developer/CommandLineTools"路徑就完成安裝

安裝 git,最流行的版控軟體

閱讀全文 »

使用 Hexo 搭配 Github Pages 建立個人 blog - 安裝筆記

發表於 2018-07-23 | 分類於 Hexo |

Google 可以找到很多 Hexo + Github Pages 安裝心得,過程中遇到幾個狀況跟注意事項就順手記下來

系統環境:macOS High Sierra 10.13.6.
Hexo v3.7.1 — NexT.Pisces v6.3.0

無法安裝 hexo? npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/usr/local/lib

npm 安裝權限問題,在安裝指令前加上 sudo 提示輸入密碼,輸入後即可安裝。

為什麼語系套用失敗?

安裝上述最新版本,language = zh-TW,翻了幾篇參考文章大多都是小寫zh-tw,大寫才能套用主題內的正體中文語系檔。

建立tags或categories未能正確產生頁面

建立頁面時,注意雙引號 "categories" 要打正確,手滑在中文狀態下輸出打成 "categories“,導致無法正常產生 categories 頁面。

動畫特效為什麼設為 true 還是沒啟用?

啟用動畫特效,記得要把正確的檔案下載到主題資料夾內,啟用主題內功能時,建議要到註解寫的網頁看一眼。
https://github.com/theme-next/theme-next-canvas-nest

閱讀全文 »
Hungkai Yang

Hungkai Yang

學無止盡

10 文章
5 分類
12 標籤
RSS
GitHub
© 2018 Hungkai Yang
由 Hexo 強力驅動 v3.7.1
|
主題 — NexT.Pisces v6.3.0