[英]Routing to custom action in controller
I'm trying to add ajax that would render a partial upon clicking a div. 我正在尝试添加一个Ajax,在单击div时将呈现部分内容。
This the link: 该链接:
<h1 id="comments_viewall"><%= link_to "View All", videos_update_comments_path, remote: true%></h1>
I have the custom method in the videos controller: 我在视频控制器中有自定义方法:
def update_comments
puts "hello"
end
And the routes are as such: 路线是这样的:
get 'videos/update_comments'
However, I get this error: 但是,我收到此错误:
GET http://localhost:3000/videos/update_comments 404 (Not Found)
Started GET "/videos/update_comments" for 127.0.0.1 at 2014-05-05 13:49:02 -0400
Processing by VideosController#show as JS
Parameters: {"id"=>"update_comments"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
in show
Video Load (0.1ms) SELECT "videos".* FROM "videos" WHERE "videos"."id" = ? LIMIT 1 [["id", "update_comments"]]
Completed 404 Not Found in 2ms
ActiveRecord::RecordNotFound (Couldn't find Video with id=update_comments):
app/controllers/videos_controller.rb:94:in `show'
I followed what bunch of stack overflow questions told me to do, but its still not working.. 我遵循了堆栈溢出问题告诉我的操作,但仍然无法正常工作。
Move get 'videos/update_comments'
above the show
route defined for videos
resource. 将get 'videos/update_comments'
移动get 'videos/update_comments'
为videos
资源定义的show
路线上方。
For example: 例如:
get 'videos/update_comments'
resources :videos
As currently, when you do a GET request for videos/update_comments
, Rails finds the first match from routes.rb and routes the request there. 与当前一样,当您对video videos/update_comments
进行GET请求时,Rails从route.rb中找到第一个匹配项,并将请求路由到那里。 So, it matches videos/:id
route and routes the request to VideosController#show
action instead of VideosController#update_comments
. 因此,它匹配VideosController#show
videos/:id
路由,并将请求路由到VideosController#show
操作,而不是VideosController#update_comments
。
You can see it clearly in the generated log 您可以在生成的日志中清楚地看到它
Started GET "/videos/update_comments" for 127.0.0.1 at 2014-05-05 13:49:02 -0400
Processing by VideosController#show as JS
By moving the update_comments
route before show
route, whenever a GET request for videos/update_comments
is made, the first match would be get 'videos/update_comments'
in your routes and the request would be directed to VideosController#update_comments
通过将update_comments
路线移动到show
路线之前,每当对videos/update_comments
进行GET请求时,第一个匹配项将在您的路线中get 'videos/update_comments'
,并且该请求将定向到VideosController#update_comments
UPDATE UPDATE
You could also define the update_comments
route within a collection
as suggested by @Addicted in the comments provided that you have defined the routes using resources :videos
您也可以按照@Addicted在注释中的建议在collection
定义update_comments
路由,前提是您已使用resources :videos
定义了路由resources :videos
resources :videos do
collection do
get 'update_comments'
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.