I’m trying to implement a login form, but my routing seems to be wrong.
When the authentication fails, the app redirects to subdomain.domain.com/login but it should only render the login page again (only subdomain.domain.com without “/login”).
views/login/index.html
<%= form_tag(check_login_path, :method => "post") do %>
... form fields
<% end %>
routes.rb
constraints(Subdomain) do
match '/' => 'login#index', :as => :login
match '/login' => 'login#check', :as => :check_login
match '/dashboard' => 'dashboard#index', :as => :dashboard
end
login_controller.rb
class LoginController < ApplicationController
def index
# some index logic
end
def check
# authenticate with mite.yo.lk account login
Mite.account = params[:domain]
Mite.authenticate(params[:email], params[:password])
if Mite.validate
redirect_to dashboard_path
else
flash[:error] = "not valid"
render :template => 'login/index'
end
end
end
In your routes, you are routing ‘/login’ to the
checkaction, and then in that action you are rendering a template for a failed validation. Rendering does not change the URL, which is why you end up with the url ‘/login’.If you want the user to see the root url after a failed login attempt, then you’ll need to change the
renderto aredirect_to :index:Alternatively, you could replace your
check_loginpath to use the root url with a POST request, and make yourloginpath route to the root only for a GET request, like so:If you do this, then you should be able to leave your current controller code as is and get the result you want.