0

I just learned about pry for debugging in rails, I'm working on a ecommerce site , on the main sign in page, when the login button is clicked, the email, username and the password field of the login form is suppose to save to the database. Instead of getting Example A, I get Example B but I'm not sure what {"controller"=> "application", "action" => "login"} means. Can someone help me please.

Example:A correct result Example:B [wrong result pasted below][1]

    23: def login
    24:   @username = params[:username]
    25:   @email = params[:email]
    26:   @password = params[:password]
 => 27:   binding.pry
    28:   if @email && @password
    29:     session[:signed_in] = true
    30:     session[:username] = params[:username]
    31:     redirect_to '/profile'
    32:     end
    33: end

[1] pry(#<ApplicationController>)> @username
=> nil
[2] pry(#<ApplicationController>)> params
=> {"controller"=>"application", "action"=>"login"}
[3] pry(#<ApplicationController>)> 

My sign in form:

                    <form action='/login' class='validate-form' method='post'>
                        <p class="checkout-coupon top log a-an">
                            <label class="l-contact">
                                Email Address
                                <em>*</em>
                            </label>
                            <input type="email">
                        </p>
                           <p class="checkout-coupon top log a-an">
                            <label class="l-contact">
                                Username
                                <em>*</em>
                            </label>
                            <br>
                            <input type="email">
                        </p>

                        <p class="checkout-coupon top-down log a-an"> 
                            <label class="l-contact">
                                password
                                <em>*</em>
                            </label>
                            <input type="password">
                        </p>
                        <div class="forgot-password1">
                            <label class="inline2">
                                <input type="checkbox" name="rememberme7">
                                Remember me! <em>*</em>
                            </label>
                            <a class="forgot-password" href="#">Forgot Your password?</a>
                        </div>
                        <p class="login-submit5">
                            <input class="button-primary" type="submit" value="login">
                        </p>
                    </form>

My routes.rb:

Rails.application.routes.draw do
 get '/profile', to:'application#profile'
 get '/logout', to: 'application#logout'
 post '/login', to:  'application#login'
end
  • You should show your form as that is what determines the params. Of course @username is nil it's assignment is commented. Additionally avoid using screenshots, please copy and paste code. – ruby_newbie Jun 25 '16 at 03:49
  • your right, yeah i commented because i was running a test but ignore it, its not commented with i'm running the code – PrideCoding Jun 25 '16 at 04:29

3 Answers3

1

In answer to your specific question

I'm not sure what {"controller"=> "application", "action" => "login"} means

This is a Ruby Hash with two key value pairs that is referenced by your link within the form. The value of the key controller refers to the specific controller that you are linking to with your form. In this case your ApplicationController as defined by the value application. The value of the key action refers to the specific method within that controller that you are calling.

Saad
  • 1,856
  • 1
  • 22
  • 28
alex
  • 5,467
  • 4
  • 33
  • 43
0

In your form you have <input type="email">, but no name attributes. Name attributes are used by the server to identify the fields in the form.

link check this for more information.

Your params attributes are just {"controller"=>"application", "action"=>"login"} because thats where the form is being submitted, To your application controller and login action, your login action is defined in your form <form action='/login' class='validate-form' method='post'> here /login, thats your action.

In this case login. To get example A, your form should be like this

<input type="email" name='email'>
<input type="text" name='username'>
<input type="password" name='password'>

If you post your form I can try to provide an answer to your other problems.

Saad
  • 1,856
  • 1
  • 22
  • 28
  • You all for the help! it worked, I'll have to educate myself on hashes, when i run "session", I get "#" what does the "not yet loaded" mean, Should i be concerned about that? – PrideCoding Jun 25 '16 at 21:05
0

Change the form to this and try again,

Try adding names to the input fields, and then use the pry and insect the params from it.

Also did you use the strong parameters for the fields?

<form action='/login' class='validate-form' method='post'>
        <p class="checkout-coupon top log a-an">
            <label class="l-contact">
                Email Address
                <em>*</em>
            </label>
            <input type="email" name="email">
        </p>
           <p class="checkout-coupon top log a-an">
            <label class="l-contact">
                Username
                <em>*</em>
            </label>
            <br>
            <input type="email" name="username">
        </p>

        <p class="checkout-coupon top-down log a-an"> 
            <label class="l-contact">
                password
                <em>*</em>
            </label>
            <input type="password" name="username">
        </p>
        <div class="forgot-password1">
            <label class="inline2">
                <input type="checkbox" name="rememberme7">
                Remember me! <em>*</em>
            </label>
            <a class="forgot-password" href="#">Forgot Your password?</a>
        </div>
        <p class="login-submit5">
            <input class="button-primary" type="submit" value="login">
        </p>
    </form>
Sravan
  • 18,467
  • 3
  • 30
  • 54
  • Thank you it worked, You all for the help! it worked, I'll have to educate myself on hashes. when i run "session", I get "#" what does the "not yet loaded" mean, Should i be concerned about that? – PrideCoding Jun 25 '16 at 21:08
  • Is the code worked now? Are you able to save all the fields into database? If my answer helped you, you can accept my answer – Sravan Jun 27 '16 at 05:08
  • Check these links regarding the session issue, [link 1](https://github.com/rails/rails/issues/10813) [link 2](http://stackoverflow.com/questions/14665275/how-force-that-session-is-loaded) – Sravan Jun 27 '16 at 05:19