mercredi 5 octobre 2016

how to use ur friendly / slug properly in rails routes?

i have a table which i generated with scaffold, something like this

rails generate scaffold Book title:string year:integer etc...

only admin can access above table with url something like :

admin/books/book-1
admin/books/book-2

i use friendlyid gem to generate slug value, and i don't have problem with that.

but now i need to access a page in public and query book data based on slug string, i really have no idea how to make route accept like this :

public/book-1

currently i do it with query string : public?book_id=123 and get the parameters like this : Book.where(:id=>params[book_id]) and i need to change it with slug way?

i tried to add this to my route file, but still no luck

# get 'public/:slug', to: 'public#detailed_book', as: 'slugged_post'

please advice.

Aucun commentaire:

Enregistrer un commentaire