in routes.rb
resources :questions, path: ''
throws errors when you are logged out.
try:
get '/:friendly_id', to: 'questions#show'
Question
I have a question model which allows users to add questions.
I have set up Friendly_id so now that when a user adds a question the url is:
http://localhost:3000/questions/this-is-a-question
However, i want the url to appear like this
http://localhost:3000/this-is-a-question
Do you have any ideas on how i can do this with friendly_id?
Thanks
UPDATE:
This is my friendly_id bit in question.rb
extend FriendlyId
friendly_id :title, use: [:slugged, :finders]
def slug_candidates
[
:title,
[:title, :id]
]
end
def should_generate_new_friendly_id?
new_record?
end
end
Solution
in routes.rb
resources :questions, path: ''
throws errors when you are logged out.
try:
get '/:friendly_id', to: 'questions#show'
OTHER TIPS
Try this in routes.rb
resources :questions, path: ''
In routes.rb
resources :questions, except: :show # Remove others you don't want
get ':id', to: 'questions#show', as: 'question'
Please make sure that you use friendly_id's reserved words if you are using a root level id. Also, make sure that you keep the get ':id', to: 'questions#show', as: 'question'
line at the bottom of your routes.rb so that everything else is prioritized above it. It's a dangerous route.
What you want in your routes.rb
is something similar to below:
get '/:id', to: 'posts#show', constraints: { id: /\d.+/ }
get '/:username', to: 'users#show'
copied from the routing documentation http://guides.rubyonrails.org/routing.html
in your case if the question name is called friendly_id
, then what you want is:
get '/:friendly_id', to: 'questions#show'
and in your controller show method
you would be using param[:friendly_id]