http://guides.rubyonrails.org/getting_started.htmlを例として使って自分のアプリケーションを作成しています。私はブログとコメントモジュールを作成するだけです。コメントやブログのコントローラーにメソッドを追加すると、新しい関数を呼び出すためのアクションをlink_toすることはできません。すべてがroutes.rbの問題を指摘していますが、私が見た新しい構文を試してみましたが、何も私のために働いていません。Rails 3コントローラにカスタムメソッドを追加する方法
私がしようとしているのは、コントローラにルビスクリプトを実行し、出力をデータベースに保存する簡単なexecuteメソッドを作成することです。すべてはチュートリアルに従って動作しますが、executeというカスタム関数でコメントコントローラを拡張しようとすると、実行することができなくなります。
comments_controller.rb #Same as destroy
def execute
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
_comment.html.erb
<%= link_to 'Execute Comment', [comment.post, comment],
:method => :execute %>
routes.rb
resources :posts do
resources :comments do
get :execute, :on => :member
end
end
rake routes |grep execute
execute_post_comment GET /posts/:post_id/comments/:id/execute(.:format) {:action=>"execute", :controller=>"comments"}
Error when I click Execute comment link:
No route matches "/posts/3/comments/6"
ありがとう、私はレールのキャストを見て、多くを学んだ。私はポストコントローラで動作する新しい機能を得ることができましたが、それでもCommentsコントローラで動作させることができませんでした。私は使用しているコードを含めるように質問を更新しました。 –
それはうまくいきました、ありがとうございました!あなたはすごいね。 –
素晴らしいリンクもあります!それが今、本当に助けになりました! – earlonrails