我尝试在登录表单中使用参数remote制作 AJAX 登录表单。但是登录后我无法将用户重定向到主页。但是重定向不起作用。我的代码如下:
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");