sixty4bit
sixty4bit

Reputation: 7956

Rails 4: how to make exceptions to wildcard routes so I an access mail previews?

In routes.rb I have the following:

get '*category/*location' => 'landing#show'
get '*category' => 'landing#show'

This is keeping me from being able to view mail previews at /rails/previews because I get redirected just as if I had typed /painting or /painting/chicago.

Using the Rails routing guides, I tried to make constraints like this:

  get '*category/*location' => 'landing#show', constraints: { category: /(?!rails)/, location: /(?!mailers)/ }
  get '*category' => 'landing#show', constraints: { category: /(?!rails\/mailers)/ }

...however, this seems to either be blocking everything except /rails/mailers (I can now open the mail previews but all of the previous dynamic behavior is broken) or it's just made the two lines null - not sure which. All I can tell is that I can now access the mail previews but the previously dynamic behavior is broken.

The other idea I had was simply to skip over the wildcard lines if I'm not in development like so:

  unless Rails.env.development?
    get '*category/*location' => 'landing#show'
    get '*category' => 'landing#show'
  end

However, this seems a bit ham-fisted. It prevents me from viewing the wildcard behavior in development, and I'm just wondering if there's a better way using constraints. Can someone help me figure out what I need to do to get those wildcards to work unless the url ends in /rails/server.

Upvotes: 1

Views: 179

Answers (1)

sixty4bit
sixty4bit

Reputation: 7956

I tried the solution in @gregates' comment (set get '/rails/previews' above the wildcards) but got the error uninitialized constant RailsController. However, the suggestion made me think to look at how the request is being processed when it works correctly, so I removed the wildcards, just accessed the mailers normally, and saw this:

Processing by Rails::MailersController#index as HTML

So I added the following to the top of my routes file and now everything works correctly:

# routes.rb
...
get '/rails/mailers' => 'rails/mailers#index'
...

Upvotes: 1

Related Questions