Rails Tutorial 3.2 Chapter 8 error: NoMethodError in SessionsController#create - railstutorial.org

I'm going through the rails tutorial and my login page is throwing an exception after exercise 8.1.5 when I click the login button with no email or pw entered:
http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec-rendering_with_a_flash_message
Error:
NoMethodError in SessionsController#create
undefined method `[]' for nil:NilClass
app/controllers/sessions_controller.rb:7:in `create'
SessionsController matches the final code exactly for the Create method
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email].downcase) #line 7
if user && User.authenticate(params[:session][:password])
#will fill this in later
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
I did change the button label to Log in instead of "Sign in" as that is too confusing with "Sign up", but I didn't think that would create the problem. sessions\new.html.erb
<% provide(:title, "Log in") %>
<h1>Log in</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(:sesssion, url: sessions_path) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit "Log in", class: "btn btn-large btn-primary" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>
This post hints that I need a method in my user model, but adding that didn't help:
NoMethodError in SessionsController#create
I tried adding this to user.rb, but it didn't help
results from find_by_email executed in function differs from console:
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return user.nil? ? nil : user
end
Any ideas would be greatly appreciated!

I've looked at the example from the book and your code and I noticed this line
if user && User.authenticate(params[:session][:password])
your User.authenticate should be lowercased to user.authenticate. Revert back to your original code.

I have just had the same problem and found that in fact i needed [:sessions] with an s on the end!
So line reads
if user && User.authenticate(params[:sessions][:password])
Hope this helps someone in the future!

I had the same problem after doing exercise 1 in chapter 8 where I replaced the use of form_for with form_tag. This resulted in the name attributes in the generated input form fields changing from name="session[email]" and name="session[password]" to name="email" and name="password". Subsequently, I needed to access the params using params[:email] and params[:password] instead of params[:session][:email] and params[:session][:password].
My new Sessions controller looks like this:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email].downcase)
if user && user.authenticate(params[:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
sign_out
redirect_to root_url
end
end
This solved the problem for me. Hope this is helpful to someone else.

In this section from the tutorial chapter 8, all instances of [:session] should be [:sessions]. Hope that helps.
class SessionsController < ApplicationController
.
.
.
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
.
.
.
end

Could you confirm the word 'session' in app/view/session/new.html.erb spelling correct?
I see you wrote:
form_for(:sesssion, url: sessions_path) do |f|
But in app/controllers/sessions_controller.rb, you wrote:
user = User.find_by_email(params[:session][:email].downcase) #line 7
They must be the same.

Related

Rails 4: undefined method `permit' for "xxxx xxxx":String

I have a search model that I hope to use in conjuction with sunspot to handle all searches across different models. I'm stuck before I even have gotne started.
Here is the form...that appears in the header of all the web pages on my site.
<%= simple_form_for #search, :url => searches_path, :method => :post do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", class: "btn btn-primary" %>
<% end %>
In my searches_controller
def search_params
params.require(:search).permit(:search)
end
In my Search.rb
attr_accessor :search
When I plug in my name into it I get this error
undefined method `permit' for "simon walsh":String
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"1Tez0pnMILEciLR6j+li+qSeO4NYBj3XsB6dYG07RymsiNUNSAGI5ztpMiD4JNAtTnqwJYdHTpPBRqGduWHjBw==",
"search"=>"simon walsh",
"commit"=>"Search"}
I am confused. This is clearly a simple error with the way I am posting the params. Any help?
Use this:
def search_params
params.require(:search).permit!
end
Instead of this:
def search_params
params.require(:search).permit(:search)
end
This issue may occur because of string parameter in params["search"], while in controller you are using .permit(:search), or maybe you have an attribute and model name that are the same.

How do I pass form variables from views to controllers in Rails 4?

I'm trying to accept telephone numbers in a simple form and I can't seem to get it to pass the variable through to the controller. The code I have so far is:
view on /users/new.erb:
<%= form_for #user do |f| %>
<%= field_set_tag do %>
<p><%= f.label :phone %><br>
<%= f.text_field :phone%>
<%= f.submit "Submit" %>
<% end %>
<% end %>
and in the controller I have:
def new
#user = User.new
end
def create
#user = User.new(params[:new])
phone = #user.phone
end
Any help would be greatly appreciated. Thanks.
There are a few problems with your code. When you write:
<%= form_for #user %>
All the form fields are sent to the controller in the form of a hash with "user" as the root. Just like
{"user": {"phone": "ENTERED PHONE NUMBER"}}
To access these fields in the controller you need to use the following method:
#phone_number = params[:user][:phone]
One More Important Thing
For security purposes rails does uses Strong Parameters. It means that you can't use the form parameters directly in Active Model for mass assignment like this:
#user = User.new(params[:user])
You need to permit all the fields explicitly for mass assignment. This can be achieved as follows.
def create
#user = User.new(params_user)
#user.save
end
private
def params_user
params.require(:user).permit(:phone)
end
params[:new] just passes the parameter :new to your #user object, but there is no params[:new] parameter because it is not defined in your form. params[:phone] should be passed to '#user' just params if you want all available params that were submitted.
Try
#user = User.new(params) #instead of #user = User.new(params[:new])
#user.phone

devise + omniauth logout

I finally got "sign in with Facebook" to work with devise/omniauth, but when a user is signed in, the "sign in with facebook" link doesn't change to "sign out" and/or there is no visible sign out option.
here is my route.rb file
devise_for :users, :controllers => {:omniauth_callbacks => "users/omniauth_callbacks", :registrations => 'registrations'}, :path_names => { :sign_in => 'login', :sign_out => 'logout' } do
get 'login' =>'devise/sessions#new', :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
get 'signup' => 'registrations#new', :as => :new_user_registration
get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
and here is the user file
class User < ActiveRecord::Base
devise :omniauthable, :omniauth_providers => [:facebook]
def self.find_for_facebook_oauth(auth, signed_in_resource=ni)
user = User.where (:provider => auth.provider, :uid => auth.uid).first
unless user
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.where
user = User.create(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20]
)
end
user
end
end
session controller:
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_path
end
def destroy
session.delete[:user_id] = nil
redirect_to root_path
end
end
Omniauth_callbacks_controller:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
and lastly, the application layout
<% if current_user %>
Signed in as <strong><%= current_user.name %></strong>!
<%= link_to "Sign out", destroy_user_session, id: "sign_out" %>
<% else %>
<li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
<% end %>
Not quite sure waht i keep doing wrong or why I'm having a hard time looking for the answer, so I thought it might be easier to just put up the code. I"m new at this, so any help is appreciated. Thanks!
Short Answer - current_user is nil so your not actually logged in.
If "Sign in with Facebook" is still showing, and Sign Out is not showing, i'm assuming the "Signed in as" Is also not showing.
This is most likely due to this current_user not being set, so this line is failing
<% if current_user %>
and so this code gets triggered
<li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
So I am assuming current_user is not actually getting set, therefore you're not really logged in. You can add the devise controller filter authenticate_user! to see if you're logged in or not. Something like
class ApplicationController
before_filter :authenticate_user!
end
Which will redirect you if you are not logged in.
Use Pry To Find Out
Pry is a great tool for debugging these kinds of things
To debug with pry, you would add to your Gemfile
group :development do
gem "pry", "~> 0.9.12.4"
end
and run bundle install
you can then add this to your code
<%= binding.pry %>
<% if current_user %>
Signed in as <strong><%= current_user.name %></strong>!
<%= link_to "Sign out", destroy_user_session, id: "sign_out" %>
<% else %>
<li><%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %></li>
<% end %>
Go through your normal Login with Facebook workflow, and the Rails Server in your terminal will 'stop' at the `<% binding.pry %> and allow you to run commands, including checking variables.
In the terminal your rails console is running in, you should see something like
current_user ? "There is a current user set" : "No current user is set"
You may also just run
current_user
but the former has more verbose output. Some other useful debugging commands include
help
whereami
exit
!!!

Why is my date_field parameter nested inside the label name?

in my controller, I have this:
def index
filter = []
if !params[:paid].blank?
paid = params[:paid]
filter << ["copay_received = '#{paid.to_s}'"]
end
if !params[:junk].blank?
junk = params[:junk][:effective_on]
filter << ["clients.effective_on >= '#{junk.to_s}'"]
end
#unpaids = Appointment.joins(:client).where(filter.join(" AND "))
end
which is based on the (non-accepted) answer to this question.
Here is the form I used on the index page to search it:
<%= form_tag("/", method: "get") do %>
<%= label_tag(:paid, "Search for:") %>
<%= check_box_tag(:paid) %>
<%= date_field(:junk, :effective_on) %>
<%= submit_tag("Search") %>
<% end %>
I finally got my date_field working (as shown with the effective_on parameter).
But my question is why do I have to go an extra layer down into the params hash to get :effective_on while I don't have to do the same thing to reach :paid?
you are using the wrong method. it should be date_field_tag.
the date_field method is supposed to be used with an object form like
form_for #user do |f|
f.date_field :born_on
end

searchresult usin sphinx

i am using sphinx beta and thinking-sphinx to perform search functionality..the search i
wann should search for the word i entered in the database.which is populated with data already.
so in my application Mydoc,i got articlesmodel and controller.. articles is db is populated with data.
i wann to search for the data in articles..so far i ve done with following things but not getting the search result
=> created new search controller
def index
#articles = Article.search params[:search]
end
=> articles.rb(model)
define_index do
indexes name, :sortable => true
indexes description
indexes title, :sortable => true
end
def self.search(search)
ThinkingSphinx::Search
end
=> searches/index.html.erb
<%= form_tag do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "search", :name => nil %>
</p>
<% end %>
BUT WEN I CLICK ON THE SEARCH BUTTON ITS ASKING FOR CREATEACTION ?????
PLEASE COULD U HELP TO GET SEARCH RESULT
Your form is making a POST request to your SearchController, and that is interpreted as a call to the create action. Try using a GET request instead with your form - something like this:
<%= form_tag '', :method => :get do %>