I’ve set the friendly_id gem up, and generated slugs for my model.
The slugs are all shown in my DB column, and if I run Event.friendly.find('my-event-name') in the console, it works perfectly.
My URL’s are still showing as organisation/1/events/1 though, when I check the result of the following link_to:-
<%= link_to organisation_event_path(event.organisation.id, event.id), class: 'btn btn-primary' do %>
I’ve also added
extend FriendlyId
friendly_id :title, use: :slugged
to my model.
When I visit the URL manually, it works with the friendly id - so it seems to be just the link_to helper that isn't working.
Could possibly be related to some additional setup required for nested routes here - but I can’t seem to find anything when I search.
resources :organisations do
resources :events
end
CodePudding user response:
This is way more explicit then you need to be. As long as you're fallowing the conventions you can let them do the work for you:
<%= link_to [event.organisation, event], class: 'btn btn-primary do %>
# ...
<% end %>
Under the covers rails will look up the organisation_event_path
helper (by looking at the model names) - and call its to_param
method.
CodePudding user response:
The answer seems to be that I need to change the .id
in my link_to, to .friendly_id
like so:-
<%= link_to organisation_event_path(event.organisation.id, event.friendly_id), class: 'btn btn-primary' do %>
Would expected it to work without this but perhaps not.