Redirection in Rails 4 routes - ruby-on-rails

My site used to have a mobile view here:
https://www.example.com/m/home
We have deprecated the mobile views and now I need a simple way to trim the /m/ off the URL so that the request proceeds to the correct page.
Example:
https://www.example.com/m/about => https://www.example.com/about
https://www.example.com/m/user/:id => https://www.example.com/user/:id
I'm hoping to solve this in the Rails routing without having to introduce a new controller action or meddle with nginx. I have 100+ routes. Thanks in advance.
Rails version: 4.2

There is a redirection module (also documented in the guide).
Something like :
get '/m/about', to: redirect('/about')
get '/m/user/:id', to: redirect('/user/%{id}')
Which you can combine with route globbing for a generic solution :
get '/m/*path', to: redirect('/%{path}')

How about just refactor your routes a bit:
Eg: Previous routes.rb
resources :users
# ...
Now, it becomes:
['m', ''].each do |sc|
scope sc do
resources :users
# ...
end
end

Related

How to handle a Rails redirect that starts with "x" pattern in the url

I am new to Ruby and Rails. So far I've successfully:
updated a url from /dashboard to /specific_dashboard
gotten the redirect working for when you put in /dashboard, it now takes you to /specific_dashboard
The problem I am running into is redirecting when there are additional params in the url like /dashboard/program... or /dashboard/dashboard_page...
Is there Ruby syntax that would be equivalent to using a wild card like get "/dashboard/*" => redirect("/specific_dashboard"). I've tried this and match '/*dashboard' => redirect("/specific_dashboard"), via: :all and can't get it to work. If its helpful, I have this code in my routes.rb file.
you can use nested routes.
Example you have authors and books.
resources :authors do
resources :books
end
https://guides.rubyonrails.org/routing.html#nested-resources
It should be the following in routes.rb:
get 'dashboard/*section', to: '<controllerName>#<actionName>'
Here:
controllerName = Controller Name, e.g. Home
actionName = Action Name, e.g. Dashboard
class HomeController
def dashboard
# inside params[:section] you'll get 'dashboard_page'
# if you're accessing '/dashboard/dashboard_page'
end
end
Reference: https://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments

How to change namespace URI in Ruby on Rails?

I'm newbie in RoR and I'd like a bit of help here.
I have the following URI:
http://localhost:3000/abouts/2
And the next in Route:
resources :abouts, only: [:show]
I'd like to show the information of "about/2" in another page, for example:
http://localhost:3000/new_about
Regards!
You can simply map the desired URL to your existing action:
get 'new_about', to: 'abouts#show', id: 2
You can add your own routes in rails. You'd do something like :
get "/new_about" => "controller#action"
I would suggest sticking to the Rails way of doing things though unless it's only a few number of pages and you specifically need them to be named differently.

How to stop Rails route preempting another

I am working on an API for a mobile app. I am running into a problem where one of my routes is preempting another. The two routes are:
api_vocabs_all GET /api/vocabs/all(.:format) api/vocabs#all
api_vocab GET /api/vocabs/:id(.:format) api/vocabs#show
Whenever I navigate to "api/vocabs/all" Rails sends the request to the show method with the id = all. Is there a way around this?
Update: Looking at my code again I noticed that while the above doesn't work the non-api version does work. Do I need to put my route inside the namespace?
vocabs_all GET /vocabs/all(.:format) vocabs#all
vocab GET /vocabs/:id(.:format) vocabs#show
Below are all my routes from routes.rb
namespace :api do
resources :vocabs
end
get 'vocabs/all' => 'vocabs#all'
get 'api/vocabs/all' => 'api/vocabs#all'
resources :vocabs
Converting my comment into an answer. Put api/vocabs/all above the namespace :api. Remember that your routing file should be ordered from most specific to least specific.
get 'api/vocabs/all' => 'api/vocabs#all'
namespace :api do
resources :vocabs
end
get 'vocabs/all' => 'vocabs#all'
resources :vocabs

Rails route redirect to a inner url

I've changed in my rails project the url from /tours/* to /tours/peru/. It's working fine but google already indexed the /tours/ url so I want to write a route that redirect the the URL to the new version of the URL
My code It's like this:
resources :tours, path: '/tours/peru' do
resources :reviews
resources :images
resources :quotes
end
match "/tours/:id" => redirect("/tours/peru/:id")
So I'm sure who to write the redirect to make it work
I think the correct syntax is:
match "/tours/:id" => redirect("/tours/peru/%{id}")
See the documentation for more info.
If you're looking for a GET redirect, I'd propose something like:
get '/tours/:id', to: redirect('/tours/peru/%{id}')
For more ruby 1.9+ syntax. Don't mean to be too picky. :)

Rails 3 Routing resources scoped to a username

I have a basic understanding of rails routing, but nothing too advanced. So far I've gotten by using the RESTful resource based routes and a few custom named routes.
I am nearly done my app now though and I wanted to make some pretty urls.
In my app, each user has many pages. What's the best way to make the URL's look like www.sitename.com/username/page_name?
This will route to the pages controller's show action. Params hash includes :username and :page_name.
match "/:username/:page_name" => "pages#show"
Remember to put it last or it will match pretty much everything.
I'm not quite sure what you're using this for, but something like this might work in your routes file:
resources :users do
get 'page_name'
end
Which will produce: users/:id/page_name
You might want to check out the Railsguide on routing.
What you are looking for is a member route (section 2.9.1).
resources :users do
member do
get :cool_page
end
end
Will result in /users/:id/cool_page

Resources