Answered. See the end of post.
I am mainly a Java developer and fairly new to Ruby on Rails. I have successfully gone through the tutorial at railstutorial.org. So I got this to work once. My problem has come up when I started going through the tutorial a second time but this time trying to replace the authentication with the Sorcery gem. My main reference for that was episode #283 on railscasts.org. Some of it was working but when I try to sign in, I get an error saying:
ActiveRecord::PreparedStatementInvalid in SessionsController#create
missing value for :email in [:email] = :login
This was a difficult thing to search for since it is quite specific but with very general words that appear in a lot of places. I think I am doing things the same way as in the original tutorial when I had it working. Below is my views/sessions/new.html.erb which is just about the same as the original tutorial.
<% provide(:title, "Sign in") %>
<h1>Sign in</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(:session, url: sessions_path) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
<%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>
And here is the create method in my sessions_controller.rb which is a little different from the tutorial because of sorcery, but I don't know why params references are not working the same way as in the tutorial:
def create
user = login(params[:session][:email], params[:session][:password])
if user
redirect_back_or_to root_url, :notice => "Logged in!"
else
flash.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
The rails cast just uses params[:email] but that was because he used form_tag instead of form_for and changed which param to use.
I'm really lost here. Not sure why it isn't working the same as the tutorial. Any help would be greatly appreciated. Let me know if I should provide any additional information. Thanks.
EDIT:
Added the full error above. It must be some problem with sorcery's login method or my database. The parameters are coming through fine. Here's the full stack trace (back to my part of the code. It's really too long beyond that.)
Stack trace:
activerecord (3.2.11) lib/active_record/sanitization.rb:141:in `block in replace_named_bind_variables'
activerecord (3.2.11) lib/active_record/sanitization.rb:135:in `gsub'
activerecord (3.2.11) lib/active_record/sanitization.rb:135:in `replace_named_bind_variables'
activerecord (3.2.11) lib/active_record/sanitization.rb:115:in `sanitize_sql_array'
activerecord (3.2.11) lib/active_record/sanitization.rb:28:in `sanitize_sql_for_conditions'
activerecord (3.2.11) lib/active_record/relation/query_methods.rb:324:in `build_where'
activerecord (3.2.11) lib/active_record/relation/query_methods.rb:136:in `where'
activerecord (3.2.11) lib/active_record/querying.rb:9:in `where'
sorcery (0.7.13) lib/sorcery/model/adapters/active_record.rb:32:in `find_by_credentials'
sorcery (0.7.13) lib/sorcery/model.rb:108:in `authenticate'
sorcery (0.7.13) lib/sorcery/controller.rb:33:in `login'
app/controllers/sessions_controller.rb:8:in `create'
I guess I'm too new to answer my own question right away. Fixed it myself:
In sorcery.rb, I had:
user.username_attribute_names = '[:email]'
Apparently, the quotes shouldn't be there. I was confused by the comments in the file. After I made that change I needed to restart the server for the change to take effect.
Fixed it myself:
In sorcery.rb, I had:
user.username_attribute_names = '[:email]'
Apparently, the quotes shouldn't be there. I was confused by the comments in the file. After I made that change I needed to restart the server for the change to take effect.