Joe Morano
Joe Morano

Reputation: 1885

Can I automatically replace characters in my urls?

I have the following route: get '/pages/:name', to: "pages#show", as: 'page' for my model Page. Can I automatically replace a certain character with another?

Specifically, if a Page has a :name containing the character "/", can I automatically replace that character with "_" or something else in the final url?

Upvotes: 1

Views: 170

Answers (1)

Darpa
Darpa

Reputation: 406

Replace

get '/pages/:name', to: "pages#show", as: 'page'

with

get '/:name', to: redirect {|path_params, _| "/pages/#{path_params[:name].gsub('/','_')}" }, as: 'page'
get '/pages/:name' => 'pages#show'

Upvotes: 1

Related Questions