Skip to content

Instantly share code, notes, and snippets.

@larzconwell
Created March 24, 2012 03:40
Show Gist options
  • Save larzconwell/2177951 to your computer and use it in GitHub Desktop.
Save larzconwell/2177951 to your computer and use it in GitHub Desktop.
###I'm trying to get Ajax working for my sign in forms, but I can't get it work. It literally does nothing!
###Here's my controller
def create
session[:user_id] = nil if session[:user_id] # Destroy any current sessions
user = User.where(email: params[:email]).first
respond_to do |format|
if !params[:bot].blank?
# If the bot hidden field has a value, assume it's a bot
format.html do
flash[:error] = "We don't like bots.."
redirect_to root_path
end
elsif user && user.authenticate(params[:password])
# 'authenticate' comes from the 'has_secure_password' method in the User model
delete_signin_error # Delete the signin_error cookies on signin
session[:user_id] = user.id # Set a session from the id
Time.zone = user.time_zone # Set the users time zone on sign in
format.html { redirect_to profile_path, notice: "You have been signed in as #{user}." }
else
signin_error # Add a signin_error cookies or increment the number by one
format.html do
flash[:error] = "There was an error, please check your email/password and try again."
redirect_to root_path
end
end
format.js
end
end
###And here is my view partial with the remote form
<%= form_tag sessions_path, remote: true do %>
<table border="0">
<tr>
<td><%= text_field_tag :email, nil, placeholder: "Email" %></td>
</tr>
<tr>
<td><%= password_field_tag :password, nil, placeholder: "Password" %></td>
</tr>
<tr>
<td>
<%= hidden_field_tag :bot, nil %>
<%= submit_tag "Sign In" %>
</td>
</tr>
</table>
<% end %>
###So is there any particular reason why it isn't working? It should redirect, but it just sits there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment