By default, Rails uses the model's id in the URL. What if you want to use the name of the model instead? You can change this behavior by overriding the to_param method in the model. Watch this episode for details.
# product.rb
def to_param
"#{id}-#{permalink}"
end
# controller
@product = Product.find(params[:id])
or
# product.rb
def to_param
permalink
end
# controller
@product = Product.find_by_permalink(params[:id])
了解如何在Rails应用中通过覆盖to_param方法来自定义模型的URL,使用模型名称而非默认的ID。
8829

被折叠的 条评论
为什么被折叠?



