【发布时间】:2010-12-15 07:18:59
【问题描述】:
我以http://guides.rubyonrails.org/getting_started.html 为例来帮助我创建自己的应用程序。我创建博客和 cmets 模块就好了。当我向 cmets 或博客控制器添加方法时,我无法让 link_to 操作来调用新函数。一切都指向 routes.rb 中的问题,但我已经尝试了所有我见过的新语法,但没有任何东西对我有用。
我要做的是在控制器中创建一个简单的执行方法来运行 ruby 脚本并将输出保存到数据库中。一切都按照教程进行,但是当我尝试使用名为 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"
【问题讨论】:
标签: ruby-on-rails-3