krish p
krish p

Reputation: 137

Call Rails Controller from Console (passing credentials and/or session ID)

app.get '/namespace/controller'

works, but how can I:

  1. Pass the credentials to a secure controller.
  2. Pass the SESSIONID for a previously authenticated user.

Upvotes: 4

Views: 1511

Answers (1)

smathy
smathy

Reputation: 27971

The app object takes care of the session for you, so you don't need to worry about handling the session id manually. So, you just login, then access other pages that depend on the session being set:

app.post app.login_path, :username => "username", :password => "password"
app.get app.other_path

You can also inspect the response and session after any request, eg.:

app.response.redirect_url
app.session[:user_id]       # assumes your login process uses the `user_id` key in your session

If you want, you can create a file called .irbrc in your $HOME directory, combining some of these ideas with something like this in it:

if Rails
  def login
    app.post app.login_path, :username => "username", :password => "password"
    "Logged in with user_id: #{app.session[:user_id]}"
  end
end

Then, in your Rails console you can just type login to issue that method, and it will return the string showing the value of the user_id session element.

Upvotes: 5

Related Questions