[英]In ruby on rails, can I add more than one resources in one controller?
在 ruby on rails 中,我可以在一個控制器中添加多個資源嗎? 因為,如果我在一個控制器中添加多個資源,那么一個視圖文件中就會有多個創建、更新、索引..動作和 html.erb 文件。 如果我想在一個控制器中設置多個資源,我該怎么辦? 那些同名動作如何成功存在於一個文件中?
你能做這個嗎? 當然。 您可以:
update_lecture
、 update_speaker
等。lecture_id
和speaker_id
。但是……你想這樣做嗎? 不!
想想邏輯,稍微簡化一下,默認情況下它是:
[controller one] -> [model one]
[controller two] -> [model two]
[..etc..]
但現在變成了:
/-> [model one]
[controller one] --
\-> [model two]
最終它會變成:
/-> [model one]
[controller one] --
\-> [model two]
/-> [model three]
[controller two] ---> [model four]
\-> [model five]
/-> [model six]
[controller three which was added later] -
\-> [model three again because oops I forgot
that it was already in controller two]
從控制器到底層模型有一個簡單易懂的明顯映射。 這很好! 當你有一個像/speaker/42
這樣的 URL 時,它可以很容易地理解所涉及的方法和類。 但是當你使用你所要求的方案時,所有的賭注都沒有了。 /speaker/42
可以在你的應用程序的任何地方結束。
還請記住,使用 Ruby on Rails 的一個巨大優勢是,基本上每個 RoR 應用程序或多或少都相似。 這太棒了,因為如果我的同事在度假,我可以啟動我以前從未見過的“他的”應用程序,並修復客戶剛剛調用的那個關鍵錯誤!
在 RoR 中做單一的東西有一些很好的理由。 但這絕對不是其中之一;-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.