I try make a AJAX login form using parameter remote in login form. But after login i can't redirect user to home page. But redirection does not work. My code below:
class SessionsController < ApplicationController
def create
user = User.find_by_login(params[:login])
respond_to do |format|
if user and user.authenticate(params[:password])
session[:user_id] = user.id
format.html { redirect_to index_path }
format.json { head :no_content }
format.js { redirect_to index_path }
else
format.html { redirect_to login_url, alert: "Wrong login or password!" }
format.json {render json: "Wrong login or password", status: :unprocessable_entity}
format.js
end
end
end
<!-- app/view/session/new.html.erb -->
<%= form_tag(nil, remote: true) do %>
<%= text_field_tag :login, params[:login] %>
<%= password_field_tag :password, params[:password] %>
<%= submit_tag "Login" %>
<% end %>
<!-- app/view/session/create.js.erb -->
$(document).window.location.replace("http://hardrate.net");
Change this line:
format.js { redirect_to index_path }
for
format.js
Edit:
Also change
$(document).window.location.replace("http://hardrate.net");
for
window.location.replace("http://hardrate.net");
or
window.location.href = "http://hardrate.net";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.