0

Given the resource Events, I want /events/1 to navigate to /events/1/column_name in the URL bar when entered. Column name is a t.string :name in the Events DB migration. This column_name will need parameterize to be called on it before redirecting. Any ideas on how to get this done?

Example:

If you navigate to https://stackoverflow.com/users/4180797 the URL will automatically become https://stackoverflow.com/users/4180797/james-lowrey after loading. Holds true for 4180797/any-other-txt-here. So "James Lowrey" would be the name column, and it would become james-lowrey after calling parameterize on it.

2
  • 1
    I'm not sure if i understood your question, could you provide an example?
    – Gerry
    Jul 10, 2017 at 20:02
  • Thanks for asking! If you navigate to stackoverflow.com/users/4180797 the URL will automatically become stackoverflow.com/users/4180797/james-lowrey after loading. Holds true for 4180797/any-other-txt-here. So "James Lowrey" would be the name column, and it would become james-lowrey after calling parameterize on it.
    – James L.
    Jul 10, 2017 at 20:07

2 Answers 2

1

Another option will be to use pushState with javascript/jquery, for example:

First, add the following script in show.html.erb

<script>
  $(document).ready(function() {
    history.pushState(null, null, "/events/<%= @event.id %>/<%= @event.name.parameterize %>");
  });
</script>

This will change the url on load every time, no matter what comes after /:id.

Second, modify routes to accept anything after /:id (and ignore it):

Rails.application.routes.draw do
  #...
  get "/events/:id/*other" => "events#show"
end

This option has the added benefit of avoiding redirects if any text after /:id do not match, it will just get the id and replace any other text after that with @event.name.parameterize in the url (without any reload).

1
  • Great solution! This avoids redirects and saves a bit of server processing. Wouldn't work if user disables JS though, so there's some trade offs there. Thanks for the answer!
    – James L.
    Jul 10, 2017 at 21:04
0

Ok this was really tricky to figure out, didn't even know I had access to a lot of these variables before.

First edit your config/routes.rb to accept id/other_stuff

Rails.application.routes.draw do
  resources :events
  get "/events/:id/*other" => "events#show" #if any txt is trailing id, also send this route to events#show
end

Next modify event_controller.show to redirect if the URL is incorrect.

  def show
    #redirect if :name is not seen in the URL
    if request.format.html? 
      name_param = @event.name.parameterize
      url = request.original_url
      id_end_indx = url.index(@event.id.to_s) + (@event.id.to_s).length + 1 #+1 for '/' character
      ##all URL txt after id does not match name.parameterize
      if url[id_end_indx..-1] != @event.name.parameterize
        redirect_to "/events/#{@event.id}/#{name_param}"
      end
    end
  end

This will result in the exact same behavior as the Stack Overflow examples gave in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.