重命名资源路由参数

时间:2014-02-07 18:57:57

标签: ruby-on-rails ruby-on-rails-4

更多的审美问题,但我们在这里:在Rails 4中,我以通常的方式覆盖to_params方法:

class Song < ActiveRecord::Base
  def to_param
    slug
  end
end

class SongsController < ApplicationController
  def show
     Song.find_by slug: params[:id]
  end
end

slug被分配给密钥:id让我很烦。原因是resources中对config/routes.rb的调用:

resources :songs

其中包括以下路线:

song GET    /songs/:id(.:format)           songs#show

有没有办法重新定义params键:id to:slug 没有手动重新定义每一条路线:)?

0 个答案:

没有答案