mdmb
mdmb

Reputation: 5283

Friendly_ID url inside a model

I am using Rails with React in my app, and I wanted to pass url for Post model in as_json method. It has been working correctly with this method found on SO, where I pass the locale and the id (or self.id):

Rails.application.routes.url_helpers.post_path(locale, id)

I have now included FriendlyID gem in my project and I can go to my post page using the generated slug, but the method above still returns the old URL with ID. My question is - how can I change that?

PS I can do just (code below), but was wondering if there is an easier way?

...posts_path(locale) + "/#{slug}"

Upvotes: 0

Views: 85

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

The correct way (and the way Rails was designed to do it) is

Rails.application.routes.url_helpers.post_path(locale, to_param)

the to_param method by default returns the :id as a string but is overridden by people who use slugs directly or via gems like friendlyID

Upvotes: 4

Related Questions