ちょうどタイミング良く@maihaさんから、@yuguiさんが
近くに来てるという情報をもらったので、
@yamazさんも交えて、ネストしたリソースを扱うコントローラの問題の答えを得るべく、ミーティングをしました。

問題の定義:

モデル層で Post has_many Comment な関係にある時に、
Commentのリストとコメント投稿フォームを含むPosts#show画面(典型的な例としてはブログの一記事表示画面)から、Commentを投稿した場合に、

  1. Comments#createで受け取ると、Commentのsaveに失敗した時に、Post#showを表示したいが、redirect resource(@comment.post) すると、@comment.errorsの情報がロストしてしまう。
  2. Posts#create_commentなどで受け取ると、Commentリソースの処理をPostsコントローラで行う事になって責任の範囲が不明確になり、格好が悪い。

解決策

Merbベースでコンセプトを示します。
まずは、Postsコントローラの中で、以下のような包含関係を宣言するようにします。

ruby>>
class Posts < Application
has_many :comments
end

class Admin < Application
has_many :comments
end
<<--

これにより、コントローラ同士の協調関係を、コントローラ自身が知っているという事になります。
具体的にhas_manyがやることは、以下のようなbeforeフィルターをshowアクションに対してセットする事です。

ruby>>
class Posts < Application
before :only => :show do
controller = Comment.new(request)
@comment = case request.method
when "POST"; controller._dispatch(:create)
when "PUT"; controller._dispatch(:update)
when "DELETE"; controller._dispatch(:destroy)
end
end
<<--

:showアクションに対して、本来は使われない"POST", "PUT", "DELETE"
の各メソッドでのリクエストがあった場合に、Commentsコントローラに処理を回します。"GET"の場合は普通にPosts#showが行われます。
Commentsコントローラ側では、メソッドの返り値としてcommentオブジェクトを返します。

ruby>>
def create(comment)
Comment.create(comment)
end
<<--

作成に失敗した場合は、@comment.errorsにエラー情報が入っているので、
Posts#showの中から利用出来ます。

Posts#show内のComment投稿フォームは以下のような感じで、
Post#showに対してサブミットします。

html>>
<%= form_for @comment, :action => resource(@post) do %>
<%= partial "comments/form" %>
<% end =%>
<<--

コンセプトなので実際に動くかどうかまだ検証してないですが、
こんな感じで良いのではないかと。

MerbだとControllerがresponseの生成を担当していないので、
コントローラをまたいだ処理のネストが高速に行えます。
Railsの場合は、個々のアクションの実行がresponseの生成を伴うので、
この方法だとオーバヘッドが大きくて難しいかもしれません。

posted by genki genki on Tue 20 Jan 2009 at 02:03 with 0 comments
Contents rssrss
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Droonga関連の記事のまとめ
RuntimeErrorの特定のメッセージに限定してrescueする方法
jQueryでscriptタグを実行せずにappendする
Tags
coffeescriptdefergroongajsmemonodenodejs
Comments rssrss
https://listji.com/index.php?page=user&action=pub_profile&id=72479&item_type=active&per_page=16 https://listji.com/index.php?page=user&action=pub_profile&id=72479&item_type=active&per_page=16: As tһe premier furniture store аnd comprehensive f... 05/16 07:16
https://khvanchkaravino.ru https://khvanchkaravino.ru: Квартира мечты за 2 месяца — помогаем выбрать мебе... 05/16 05:06
https://www.tequilagames.com https://www.tequilagames.com: Thanks for finally writing about >something new an... 05/16 03:16
https://%EB%B6%80%EB%B9%84%EC%A3%BC%EC%86%8C.isweb.co.kr/ https://%EB%B6%80%EB%B9%84%EC%A3%BC%EC%86%8C.isweb.co.kr/: We absolutely love your blog and find a lot of you... 05/16 01:40
https://zgarcitul01.com https://zgarcitul01.com: Hi my loved one! I wish to say that this post is a... 05/16 01:10
https://pelnadobrego.pl https://pelnadobrego.pl: I love what you guys tend to be up too. This sort ... 05/15 22:11
https://bet365info.org/%f0%9f%92%b8-%d8%a8%d8%a7-%d9%87%db%8c%da%86%db%8c-%d8%af%d8%b1-%d8%a7%d9%86%d9%81%d8%ac%d8%a7%d8%b1-%d9%85%db%8c%d9%84%db%8c%d9%88%d9%86%d8%b1-%d8%b4%d9%88-%d8%b1%d8%a7%d9%87%d9%86%d9%85%d8%a7%db%8c/ https://bet365info.org/%f0%9f%92%b8-%d8%a8%d8%a7-%d9%87%db%8c%da%86%db%8c-%d8%af%d8%b1-%d8%a7%d9%86%d9%81%d8%ac%d8%a7%d8%b1-%d9%85%db%8c%d9%84%db%8c%d9%88%d9%86%d8%b1-%d8%b4%d9%88-%d8%b1%d8%a7%d9%87%d9%86%d9%85%d8%a7%db%8c/: درود، بنده امروز اتفاقی در اینترنت با این وبسایت آ... 05/15 21:26
https://linklist.bio/jutawanbetlogin https://linklist.bio/jutawanbetlogin: Hi there, You've done an excellent job. I will ce... 05/15 21:20