开发者

How do you route to a show method using an alternative name?

I'm working on a Rails 3 project with the model called "recover"

I've created the following route that works. It routes to the index as I expect:

match 'checkedin' => 'recovers#index', :as => 'checkedin'

link_to 'index', checkedin_path

How do I route to the id of the show method? The following code is my attempt, but it does not function. What am I doing wrong?

match 'checkedin/:id' => 'rec开发者_Python百科overs#show', :as =>'checkedin_show'

link_to 'show', checkedin_show_path(@recovers)


Could it be you meant to write

link_to 'show', checkedin_show_path(@recover)

Notice the argument is @recover (without the "s").


David Sulc's answer seems like the right one. My question is "Why aren't you using resources for this?"

Your route would be an easy resources :recovers, :path => 'checkedin' (... and you should add an underscore to checked_in to match typical Rails conventions). You'd get these routes:

    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)       


you have to create method

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

or

on control page that you create

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜