如何使用替代名称路由到show方法?

时间:2011-01-14 17:54:14

标签: ruby-on-rails routing

我正在开发一个名为“recover”的模型的Rails 3项目

我创建了以下有效的路线。它按照我的预期路由到索引:

  

匹配'checkedin'=> “恢复#指数”,   :as => 'checkedin'

     

link_to'index',checkedin_path

如何路由到show方法的id?以下代码是我的尝试,但它不起作用。我做错了什么?

  

匹配'checkedin /:id'=>   '恢复#show',:as   => 'checkedin_show'
  link_to'show',checkedin_show_path(@recovers)

3 个答案:

答案 0 :(得分:1)

可能是你想写的吗

link_to 'show', checkedin_show_path(@recover)

注意参数是@recover(没有“s”)。

答案 1 :(得分:0)

David Sulc的答案似乎是正确的答案。我的问题是“为什么你不为此使用资源?”

您的路线很简单resources :recovers, :path => 'checkedin'(...您应该在checked_in中添加下划线以匹配典型的Rails约定)。你会得到这些路线:

    recovers GET    /checkedin(.:format)           
             POST   /checkedin(.:format)           
 new_recover GET    /checkedin/new(.:format)       
edit_recover GET    /checkedin/:id/edit(.:format) 
     recover GET    /checkedin/:id(.:format)       
             PUT    /checkedin/:id(.:format)       
             DELETE /checkedin/:id(.:format)       

答案 2 :(得分:0)

你必须创建方法

def show 
@post = Post.find(params[:id])
end

在您创建的控制页面上

before_action :set_post, only: [:show, :edit]