How do I remove a subdomain from URL? - ruby-on-rails

I followed the Railscast on adding subdomains to a Rails app, here, and everything is working great with the subdomains. Now I just can't figure out a way to link back to the root domain without a subdomain if the requested subdomain does not exist.
I've tried adding the following to my application_controller.rb file
redirect_to root_path(subdomain: false) if #city_or_state.nil?
where #city_or_state determines weather the requested subdomain is valid. The redirect_to goes back to the root, but does not remove the subdomain.
For example, if a user tries to go to invalid.domain.com they are redirected to the root, but subdomain is not removed.
I'm trying to get invalid.domain.com to redirect to domain.com

Ok, I got it. I had to use the following: redirect_to root_url(:host => request.domain)
When I tried root_path(:host => request.domain) it didn't work. Only works with root_url.
I found the answer on some comments from Daniel Kehoe here
When attempting to visit a domain that does not exist, the invalid subdomain is removed from the URL.

You can try this way i think it works!
In the application_controller.rb you can add a before_filter :validate_subdomain
Then you add this code to the controller:
private
def validate_subdomain
# #city_or_state must be initialized before this
if #city_or_state.nil?
redirect_to request.domain
end
end

Related

How to redirect to a subdomain using lvh

I have the url like this orgname.lvh.me:3000 it's working fine, and well. Now I need to redirect to this url after a specific action. How do I do this
I have tried this
root_path(subdomain: "orgname")
But this is simply going to localhost:3000. But instead I want to redirect to the url like
orgname.lvh.me:3000
Update:
I have tried this string interpolation
def after_confirmation_path_for(resource_name, resource)
byebug
"#{resource.name}.lvh.me:3000"
#TODO redirect to a subdomain resource.name is giving the organization name
end
I ended up like this
You can simply pass the url as redirect.
redirect_to 'http://orgname.lvh.me:3000'
I had the same problem, I tried a bunch of different methods, but turns out is really simple.
Instead of using root_path, use root_url, overriding the devise method after_sign_in_path_for like this.
#application_controller.rb
def after_sign_in_path_for(resource)
root_url(subdomain: "orgname")
end
I hope it helped you.

Redirect rails devise after sign up to specific route

I am trying to add redirect, so after registration, user should redirect to user edit. But it is redirecting to root link.
The code is
def after_sign_up_path_for(resource)
redirect_to edit_user_path(resource)
end
This was working fine but now it is redirecting to root route. How can I remove this issue.
The actual path is returned in after_sign_up_path_for method. So redirect_to in it is not needed.
If 'edit_user_path(resource)' exists, then the following should work
def after_sign_up_path_for(resource)
edit_user_path(resource)
end
You have to create a RegistrationsController to over ride Devise's after_sign_up method. These are good instructions.
Take into consideration that if the account has to be confirmed by email first, you have to define the after_inactive_sign_up_path_for method. Devise's defaults are the application's root.

Path to redirect to the same page in Rails?

If "root_path" takes you to the root page, what's the equivalent path to redirect you to the same page?
Here's the context: I'm using Devise for logging in users on a Rails 4.0 app, and my "custom_failure.rb" file currently redirects the user to the root page if login is unsuccessful.
Right now, a failed login will take the user to the root path. But the login form is on every page on the site, and if someone makes a failed login on page X, I want that user to stay on page X (or at least be redirected back to it).
Here's my "custom_failure.rb" file right now:
class CustomFailure < Devise::FailureApp
def redirect_url
root_path
end
.
.
end
What should I type in to replace "root_path"?
you can use request.referrer to redirect to the same page
you can use redirect_to :back
Method. After executing the controller, it will reload the same page.
Have you tried redirect_back_or_to signin_path ? It might help.

devise: adding notice after sign up (with confirmable)

I added :confirmable to my Rails app subsequently. The problem is that when I sign up after adding :confirmable I don't get a notice displayed after the sign up for with telling me what happened, for instance:
You will receive an email with instructions about how to confirm your account in a few minutes.
Why doesn't notice appear and how can I add that notice after adding :confirmable?
Thanks for help
Notice does not appear because the devise is redirecting to your root path which is probably protected by devise authentication. When you hit root_path, you get redirected back to sign_in page (because devise couldnt sign-in the user since it is not activated yet). You can verify that by looking on your development log after you enter user information and hit "sign-up" button - you will see in the log one request for registering a user, then a request navigating to your root url (whatever is in your routes.rb) and then redirect navigation to sign_in page because of authentication.
During redirect all flash messages are lost (since flash messages are only valid for next request only) and when you get redirected from root_path to sign_in page, you are making to requests. So you either need to use flash.keep on the first request before it gets redirected, or change the after_sign_up path so redirection does not happen. I recommend changing after_sign_up path since it's easier and looks as a right way to go about it.
To do that, you need to use your own controller for registrations and add after_sign_up_path method that returns url for redirect:
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
private
def after_inactive_sign_up_path_for(resource)
new_user_session_path
end
end
#config/routes.rb
devise_for :users, :controllers => { :registrations => "registrations" }
I also recommend reading similar question to yours: Rails 3 and Devise: Redirecting to page following signup (confirmable)

Devise gem in Rails: generate user_root_path

Trying to redirect users to their associated 'home' page after successful login w/out nil'ing out stored_location_for(resource_or_scope)...which is giving me some endless redirect loops (pretty sure I've set it up wrong).
Regardless, I'm looking for a better approach...
Devise's docs state: After
signing in a user, confirming the
account or updating the password,
Devise will look for a scoped root
path to redirect. Example: For a
:user resource, it will use
user_root_path if it exists,
otherwise default root_path will be
used. This means that you need to set
the root inside your routes: root :to => "home"
I'm sorta a newbie...how does one go about generating this home_root_path for each user?
rDocs also mention:
-- (Object) after_sign_in_path_for(resource_or_scope)
The default url to be used after
signing in. This is used by all Devise
controllers and you can overwrite it
in your ApplicationController to
provide a custom hook for a custom
resource.
By default, it first tries to find a resource_root_path, otherwise
it uses the root path. For a user
scope, you can define the default url
in the following way:
map.user_root '/users', :controller => 'users' # creates user_root_path
map.namespace :user do |user|
user.root :controller => 'users' # creates user_root_path
end
but these just gives me undefined local variable or methodmap' for #ActionDispatch::Routing::Mapper:…` errors.
If you would like to redirect using a route in answer to your question below:
how does one go about generating this home_root_path for each user?
This will work if you place it in your config/routes file. It will redirect a user to articles#index after (for example) a successful confirmation.
# Rails 4+
get 'user_root' => 'articles#index', as: :user_root
# Rails 3
match 'user_root' => 'articles#index', as: :user_root
See Devise: Controllers Filters and Helpers
You could try something like this:
application_controller.rb:
def after_sign_in_path_for(resource_or_scope)
# return home_page_path for user using current_user method
end
Dug around a bit to figure out the same thing. #polarblau's answer is correct,
def after_sign_in_path_for(resource_or_scope)
user_info_path(current_user)
end
where user_info_path is the path to the page you wish to display.
Also, I would allow this to fall back to super just in case, although I'm not entirely sure if that is necessary...
def after_sign_in_path_for(resource)
if resource.is_a(User)
user_info_path(resource)
else
super
end
end
I spent several hours trying to get the same functionality, and this is the code that ended up working for me:
def after_sign_in_path_for(resource)
current_user
end
If I ever tried current_user_path, I always got undefined local variable or method current_user_path errors.
Also, I'm using Rails 3.2.8 and Devise 2.1.2.
Hope that helps.
Based on #SnapShot answer, this worked for me. I'm using multiple devise models, trying to redirect back to the users profile edit page.
get 'user_root', to: redirect('/users/edit'), as: :user_root
ROR 7 answer
get '/users/home' => 'application#test', as: :user_root

Resources