Rails routes with :name instead of :id url parameters

前端 未结 7 1935
渐次进展
渐次进展 2020-12-05 13:05

I have a controller named \'companies\' and rather than the urls for each company being denoted with an :id I\'d like to have the url use their :name such as: url/comp

相关标签:
7条回答
  • 2020-12-05 14:03

    Model.find(primary_key)
    The default parameter here is primary_key id. If you want to use other columns, you should use Model.find_by_xxx so here it could be

    def show
      @company = Company.find_by_name(params[:name])
    end
    
    0 讨论(0)
提交回复
热议问题