Frage

I have a Clips Controller where:

def set_clip
  @clip = Clip.find(params[:id])
end

My Clip has a :title attribute which users can set when creating.

I'm trying to achieve that in the URL the clip will be shown as:

http://localhost:3000/clip/1/:title

Plus, i need to get gsub involved so those ugly %20 wont show up on every Space

Can anybody enlighten me on this ?

I'm Searching for a Solution without FriendlyID

War es hilfreich?

Lösung

Assuming the /1/ is the ID of the record, having the :title as an extra in the url is redundent.

A similar (and simple) method of achieving the same result is to define to_param on the model:

def to_param
    "#{id}-{title}"
end

this would then give you a url in the form of "clip/1-title", which will work fine with the active record find(param[:id]) method

Addionally, a safer param string would be to parameterize the title with:

"#{id}-{title.parameterize}"
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top