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

前端 未结 7 1784
你的背包
你的背包 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:40

    I'd have to experiment a bit to get it right, but there's two primary parts to the solution.

    1) Add a route. in config/routes, add a line that sends requests of the form baseurl/controller/:vendor-name to the action showsummary, (or maybe a new action, show_summary_by_vendor_name) [also, if you planned on using baseurl/:vendorname, that's fine too] For convenience, make sure the parameter is something like :vendor-name, not the default :id

    2) Write the controller action. In the controller file, either edit your showsummary action to differentiate based on whether it's called with an id or with a vendorname, or just write a show_summary_by_vendor_name. (depending on best practices, and what route you wrote in 1. I don't know off the top of my head which is preferable) You can then do @vendor = Vendors.find_by_name(params[:vendor_name]) or something like that, and then render it the way you would in regular showsummary.

    3) Use that as the link. Once you confirm that baseurl[/controller?]/vendor-name works, and shows the summary, make sure all the links in your application, and elsewhere, use that link. Off the top of my head, I can't remember how difficult it is to integrate a custom route into link_to, but I think it's doable. Most search engines [google] rely heavily on links, so good SEO will have you using those named links, not the numbered ones. I think. I don't know much about SEO.

提交回复
热议问题