2014-05-05 100 views
1

我想添加一个点击div时会呈现部分的ajax。路由到控制器中的自定义动作

此链接:

<h1 id="comments_viewall"><%= link_to "View All", videos_update_comments_path, remote: true%></h1> 

我在视频控制器中的自定义方法:

def update_comments 
    puts "hello" 
end 

这些路由这样:

get 'videos/update_comments' 

不过,我得到此错误:

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' 

我跟着什么一堆堆栈溢出的问题告诉我这样做,但它仍然没有工作..

+1

安置自己的'show'方法和'耙routes'输出定义的路由。 – Pavan

+0

@vicli我的答案是否解决了您的问题?让我知道结果和查询,如果有的话。 –

回答

2

videos资源定义的show路线上述移动get 'videos/update_comments'

例如:

get 'videos/update_comments' 
resources :videos 

至于目前,当你为videos/update_comments做一个GET请求,Rails的发现从routes.rb中和将请求路由那里的第一场比赛。因此,它匹配videos/:id路由并将请求路由到VideosController#show而不是VideosController#update_comments

您可以在生成的日志

Started GET "/videos/update_comments" for 127.0.0.1 at 2014-05-05 13:49:02 -0400 
Processing by VideosController#show as JS 

清楚地看到它通过移动update_comments路线前show路线,每当videos/update_comments一个GET请求时,第一场比赛将是你的路线和要求get 'videos/update_comments'将被引导到VideosController#update_comments

UPDATE

你也定义一个collectionupdate_comments路线,通过所提供的意见@Addicted建议您使用resources :videos

resources :videos do 
    collection do 
     get 'update_comments' 
    end 
    end 
+1

由于视频控制器存在并且相应的路由似乎也存在,因此为什么不在收集下添加update_comments。 – Addicted

+0

这是一个不错的选择。我刚刚给出了一个通用示例,因为OP尚未分享如何实际定义路由。 –

+0

哇......就是这样。 – Addicted