Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

請問如果寫一個可以上傳多張照片的部落格,建議用哪種架構? #21

Closed
afgnsu opened this issue Sep 27, 2015 · 7 comments
Labels

Comments

@afgnsu
Copy link

afgnsu commented Sep 27, 2015

如題,假設我用rails g scaffold blog name content:text 產生部落格,照片搭配 Carrierwave套件,究竟該跟Rails101s的留言一樣,把照片獨立出來,然後用巢狀(nest)路由呢?還是乾脆簡單把照片用p1、p2、p3...的方式加入 blog 這個Model裡比較簡單方便? 因為我看到網路上是用另一種方式
accepts_nested_attributes_for :photos, allow_destroy: true

但是不太懂用法,可以解釋一下嗎?

https://u.osu.edu/hasnan.1/2014/03/30/rails-4-multiple-file-upload-with-carrierwave-nested-form-and-jquery-file-upload/

@tubaxenor
Copy link

端看你希望怎麼讓rails幫你處理上傳的request,如果是一個form裡頭多個照片檔案(一個POST request),那你就必須設 accepts_nested_attributes_for

另外一種做法是多個request,即是每上傳一個照片就產生一個Photo record,Form submit的時候再把這些Photo一個個加進blog post.

若有速度考量,第二種方式可以async處理,第一種比較沒有辦法,若request包含太多大容量檔案容易花很多時間甚至是timeout。

@afgnsu
Copy link
Author

afgnsu commented Sep 28, 2015

其實是沒使用過這種方式,才會上來問問大家的意思,如果用Rails101s的巢狀路由的方法,好像變得綁手綁腳的,主要我還要用來新增或修改多張照片。

@afgnsu afgnsu closed this as completed Sep 28, 2015
@hechien
Copy link

hechien commented Sep 29, 2015

@afgnsu check nested_form.

accepts_nested_attributes_for 對於新增子元素來說是一個非常方便的方式,它會幫你在儲存主物件的時候連同子物件一起存進去

所以當你有一篇文章,而該篇文章有多圖片的時候,用這種方式最為方便

@afgnsu
Copy link
Author

afgnsu commented Sep 30, 2015

新增很容易,但是想請問一下要如何再次新增/編輯?因為看起來路由是沒有關聯 :P

假設我要再次新增,我該如何讓新增的圖片同屬於原本的部落格文章?用 hidden_field 欄位嗎?

@hechien
Copy link

hechien commented Sep 30, 2015

@afgnsu
Copy link
Author

afgnsu commented Sep 30, 2015

謝謝,我馬上試試 ^_^

@afgnsu
Copy link
Author

afgnsu commented Sep 30, 2015

最後用 cocoon 套件排除 XD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants