how do I make the URL's in Ruby on Rails SEO friendly knowing a @vendor.name?

前端 未结 7 2008
你的背包
你的背包 2021-02-04 12:06

My application is in RoR

I have an action/view called showsummary where the ID has been passed into the URL, and the controller has used that to instantiate @vendor wher

7条回答
  •  感动是毒
    2021-02-04 12:18

    Ryan Bates has a great screencast on this very subject.

    Basically you overload the to_param method in the Vendor model.

       def to_param
         permalink
       end
    

    Then when you look up the resource in your controller you do something like this:

       @vender = Vender.find_by_name(params[:id])
    

    But the problem with this is that you'll have to make sure that the vendors' names are unique. If they can't be then do the other solution that Ryan suggests where he prepends the the id to the name and then parses the resulting uri to find the item id.

提交回复
热议问题