Question

I'm using friendly_id for my routes in my rails app.

I want to ensure that a page cannot be visited by the numeric id.

A page is accessible like this - www.myurl.com/myfriendlyidstub

The page should not be accessible like this - www.myurl.com/12345 (12345 is the id of myfriendlyidstub)

I can't see a way to do this in friendly_id's docs.

So I'm considering adding a method in my controller to check whether the param is numeric

def edit
    if params[:id].is_numeric? #I need to know how to write this method
        not_found #not_found redirects to a 404
    end 
    #rest of the edit action
end

Two questions -

How do I write the 'is_numeric' method?

Is this a good way of doing things? Can you see any pitfalls?

Was it helpful?

Solution

I'd use a before_filter before sticking it in every method.

You may also configure a routing segment constraint.

OTHER TIPS

It sounds like you just want to have user names as a permalink right after the root '/'...

config/route.rb
...
match '/:id' => 'controller#show'
...

After that I believe if you setup Friendly It correctly it should display as a name instead of an id.

btw thanks for finding friendly_id, I was about to start looking for something like that for my project.

Let me know if that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top