Mateusz Urbański
Mateusz Urbański

Reputation: 7882

Redirecting in Rails routes

I have simply route in my rails application that looks like this:

  resources :users, only: :show

And now for example I want to redirect to http://no_present_path.com when user with sended id is not present and when user with seneded id is present redirect to http://present_path.com. Is it any way to do this with routes constraints?

Upvotes: 0

Views: 34

Answers (1)

Piotr Kruczek
Piotr Kruczek

Reputation: 2390

Routes are meant as a simple match between a string representing a part of a url, method and an action within a controller. The best way to achieve what you're after is using a before_action in your controller. Example

class UsersController < ApplicationController
  before_action :authenticate_user, only: [:show]

  def show
    ...
  end

  private

  def authenticate_user
    redirect_to some_other_path unless id_correct?
  end
end

Upvotes: 3

Related Questions