# How to log in from console?

**URL:** <https://www.ruby-forum.com/t/how-to-log-in-from-console/155770>\
**Category:** Rails\
**Created:** [December 16, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/how-to-log-in-from-console/155770 "2008-12-16T16:25:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 16, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/how-to-log-in-from-console/155770/1 "2008-12-16T16:25:18Z")

</div>

I am attempting to investigate the object state during a login process.  
I need instruction in the correct technique to emulate filling out an  
html form and submitting it within the rails console.

Given a login form at /user\_session/new with fields :username and  
:password and a submit button.

$ script/console  
…

> > app.new\_user\_session\_path  
> > =\> “/user\_session/new”
> 
> > @params = { :password =\> ‘mypassword’, :username =\> ‘myuser’ }  
> > =\> {:password=\>“mypassword”, :username=\>“myuser”}

Now, how do I submit this to the create action of the  
user\_sessions\_controller?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 16, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/how-to-log-in-from-console/155770/2 "2008-12-16T17:57:59Z")

</div>

It will be easier to debug your app to find out the internal behavior  
of your objects.

> **[DATANOISE.COM](https://www.datanoise.com/)**
>
> DATANOISE.COM - Contact us for any business inquiries

Daniel

On Dec 16, 10:25 am, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 16, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/how-to-log-in-from-console/155770/3 "2008-12-16T18:43:55Z")

</div>

Daniel wrote:

> It will be easier to debug your app to find out the internal behavior  
> of your objects.
> 
> [http://www.datanoise.com/ruby-debug](http://www.datanoise.com/ruby-debug)
> 
> Daniel

Perhaps. But I would still like to know how to get this to work through  
the console. I have found a few leads on the web, such as :  
[http://snippets.dzone.com/posts/show/600](http://snippets.dzone.com/posts/show/600). But, I am afraid that this  
example does not make a lot of sense to me and appears at first blush to  
be a bit overwrought.

I think that my main problem at this point is my lack of familiarity  
with some basic Ruby syntax pertaining to hashes or how to construct an  
http request with form data.

If I do this:

$ script/console

> > app.new\_user\_session\_path  
> > =\> "/user\_session/new

That seems correct. Next I need to fill in the form params which are  
nested in a hash called user\_session. This is where I believe that I am  
going off the rails (pardon the pun). The user\_session hash is accessed  
in the create method of the user\_sessions\_controller thus:

@user\_session = UserSession.new(params[:user\_session])

:user\_session contains three elements, :password, :username, and  
:remember\_me. So I tried this:

> > @params = { :user\_session =\> { :password =\> ‘mypass’, :username =\> ‘myname’, :remember\_me =\> ‘0’ } }  
> > =\> {:user\_session=\>{:remember\_me=\>“0”, :password=\>“mypass”,  
> > :username=\>“myname”}}

Which also seem ok to me, given I believe that it is @params that I use  
to pass the form data back. If I misapprehend that, admittedly a strong  
possibility, then the failure that follows is expected.

Then I do this:

> > app.post app.user\_session\_path  
> > ==\> 200

But, the response code only refers to the re-rendered input page which  
has these errors:

> > app.response  
> > =\> …

## 2 errors prohibited this user session from being saved

There were problems with the following fields:

- Username can not be blank
- Password can not be blank

...

Evidently I am not getting the form data to the controller. So, if some  
kind soul would care to demonstrate to me how this is accomplished  
within the console then I will be most grateful.

Regards,

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 16, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/how-to-log-in-from-console/155770/4 "2008-12-16T18:53:19Z")

</div>

James B. wrote:

> Evidently I am not getting the form data to the controller. So, if some  
> kind soul would care to demonstrate to me how this is accomplished  
> within the console then I will be most grateful.

I figured it out.

$ script/console

> > app.new\_user\_session\_path  
> > =\> “/user\_session/new”
> 
> > @params = { :user\_session =\> { :password =\> ‘mypass’, :username =\> ‘myname’, :remember\_me =\> ‘0’ } }  
> > =\> {:user\_session=\>{:remember\_me=\>“0”, :password=\>“mypass”,  
> > :username=\>“myname”}
> 
> > app.post app.user\_session\_path @params  
> > =\> 200

Ta Daa… we’re in.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 16, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/how-to-log-in-from-console/155770/5 "2008-12-16T18:55:16Z")

</div>

James B. wrote:

s/b

> > > app.post app.user\_session\_path @params  
> > > =\> 302

Got to watch where I am snipping from…

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 16, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/how-to-log-in-from-console/155770/6 "2008-12-16T19:15:23Z")

</div>

On Dec 16, 2008, at 11:43 AM, James B. wrote:

> Perhaps. But I would still like to know how to get this to work  
> through  
> the console. I have found a few leads on the web, such as :  
> [http://snippets.dzone.com/posts/show/600](http://snippets.dzone.com/posts/show/600). But, I am afraid that this  
> example does not make a lot of sense to me and appears at first  
> blush to  
> be a bit overwrought.

I think you’d be better off creating a second way to log in where you  
can simply pass normal parameters from the console.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 16, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/how-to-log-in-from-console/155770/7 "2008-12-16T19:21:11Z")

</div>

Steven Budrys wrote:

> I think you’d be better off creating a second way to log in where you  
> can simply pass normal parameters from the console.

However, that was my problem to begin with, not knowing how to pass  
parameters from the console.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 16, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/how-to-log-in-from-console/155770/8 "2008-12-16T19:55:16Z")

</div>

On Dec 16, 2008, at 12:21 PM, James B. wrote:

> Steven Budrys wrote:
> 
> > I think you’d be better off creating a second way to log in where you  
> > can simply pass normal parameters from the console.
> 
> However, that was my problem to begin with, not knowing how to pass  
> parameters from the console.

Sorry, I should have been more clear. I meant create a new method that  
took parameters so that you could call it simply by something like

MyController.console\_login(name, password)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 17, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/how-to-log-in-from-console/155770/9 "2008-12-17T13:58:38Z")

</div>

James,

You can use Mechanize for this sort of things:

[http://github.com/tenderlove/mechanize/tree/master](http://github.com/tenderlove/mechanize/tree/master)

Cheers, Sazima

On Dec 16, 1:25 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
