0

I am looking deeper into the login process with Sinatra.

Login.erb

<div id="flash" class="failure">
<h2>  <%= flash[:failure] %> </h2>
</div>


<a href="/welcome">Go Back</a>


<% if @user.nil? == false %>

<% if @user.errors.any? %>

<%= erb 'shared/_error_messages'.to_sym %>

<form action="/users/signin" method="post">
<p> User Login</p>

<label> Email <input type="text" name="[:session][:email]"/> </label></br>
<label> Password <input type="password" name="[:session][:password]"/> </label></br>

<input type="submit" value="Log in">

</form>
<% end %>

<% else %>

<form action="/users/signin" method="post">
<p> User Login</p>
<label> Email <input type="text" name="[:session][:email]"/> </label></br>
<label> Password <input type="password" name="[:session][:password]"/> </label></br>
<input type="submit" value="Log in">

</form>

<% end %>

When I use a binding.pry to determine what happens when we submit the post request I see that I will get the following:

[1] pry(#<Sinatra::Application>)> params
{
    ":session" => {
           ":email" => "tasha@gmail.com",
        ":password" => "123"
    }
}

I do know that I have a hash with the key-value pair.

I was wondering why do I need to put quotations around the symbols in the brackets instead of having it stand alone to pull the value out? Also, why if I named it without the double quotes why did it add the double quotes when I request the params in pry?

[2] pry(#<Sinatra::Application>)> params[:session]
nil
 [3] pry(#<Sinatra::Application>)> params[":session"]
 {
       ":email" => "tasha@gmail.com",
     ":password" => "123"
 }
[4] pry(#<Sinatra::Application>)> params[:session][:email]
 NoMethodError: undefined method `[]' for nil:NilClass
from (pry):31:in `block in <main>'
[5] pry(#<Sinatra::Application>)> params[":session"][":email"]
"tasha@gmail.com"
[6] pry(#<Sinatra::Application>)> 
Tasha
  • 153
  • 2
  • 9

2 Answers2

2

You field name for email is the string [:session][:email], which gets parsed into the hash, with string type keys:

{
  ":session" => {
    ":email" => value
  }
}

These string keys start with a colon. They are not symbols!

And params[:session] will pull from params["session"]. This is because the string version of a symbol does not include the : prefix.

:session.to_s #=> "session"

You probably just want this in your template.

name="[session][email]"
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • Thank you!! I was wondering about that. For some reason, I see that people like to put the colons within the brackets but I guess it isn't needed. – Tasha Mar 20 '14 at 19:24
  • You should read up on [the difference between strings and symbols.](http://stackoverflow.com/questions/16621073/when-to-use-symbols-versus-strings-in-ruby) It's important to understand in ruby. – Alex Wayne Mar 20 '14 at 20:15
1

Notice the keys in your current params hash.

params
{
    ":session" => {
           ":email" => "tasha@gmail.com",
        ":password" => "123"
    }
}

You need to access keys in the params as they are defined. For example: ":session"

The problem is that your hash is not generated correctly.

Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108
  • Thank you, should I change the way I name it? – Tasha Mar 20 '14 at 19:22
  • Yes, the problem is with `:` prefix that you have added in the erb file. Please update the erb as suggested by @AlexWayne. That should fix incorrect key generation problem. – Kirti Thorat Mar 20 '14 at 19:29