Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8309151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:01:19+00:00 2026-06-08T19:01:19+00:00

I have taken the Rails 3 Tutorial app by Michael Hartl and have expanded

  • 0

I have taken the Rails 3 Tutorial app by Michael Hartl and have expanded on it in several areas. However, I kept the login and session handling the same. I would like to interface with an iphone app, but am not sure how. I’ve looked at RestKit and Objective Resource, but thought I would roll my own. I’ve been testing it out with cURL, but have had no luck so far. I’ve been using this command

curl -H 'Content-Type: application/json'   -H 'Accept: application/json'   -X POST http://www.example.com/signin   -d "{'session' : { 'email' : 'email@gmail.com', 'password' : 'pwd'}}"   -c cookie

As in the Rails 3 tutorial, I’m using Sessions.

These are the routes:

match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy' 

This is the controller:

class SessionsController < ApplicationController
def new
@title = "Sign in"
end

def create
user = User.authenticate(params[:session][:email],
                         params[:session][:password])
if user.nil?
    flash.now[:error] = "Invalid email/password combination."
    @title = "Sign in"
    render 'new'
else
    sign_in user
    redirect_back_or user
end
end

def destroy
sign_out
redirect_to root_path
end
end 

There is no model and you sign in with a form. Here is the html for the form:

<h1>Sign In</h1>
<%= form_for(:session, :url => sessions_path) do |f| %>
<div class="field">
<%= f.label :email %></br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %></br>
<%= f.password_field :password %>
</div>
<div class="actions">
<%= f.submit "Sign in" %>
</div>
<% end %>

<p> New user? <%= link_to "Sign up now!", signup_path %></p> 

Sorry if this is too much information, I wanted to give as much as possible.

Basically, I would like to be able to access my Rails database from a native iphone app. If someone has good advice on how to sign in, store a session, and then make other calls to the website, I would greatly appreciate it.

However, if this isn’t possible, a working cURL request would probably get me going in the right direction. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:01:22+00:00Added an answer on June 8, 2026 at 7:01 pm

    I was facing a similar situation, which led me to draw up this stackoverflow post:

    [http://stackoverflow.com/questions/7997009/rails-3-basic-http-authentication-vs-authentication-token-with-iphone][1]
    

    Basically, you can use basic http authentication with rails to simplify things.

    Here’s an example of the controller:

     class PagesController < ApplicationController  
    
      def login
        respond_to do |format|
          format.json {
            if params[:user] and
               params[:user][:email] and
               params[:user][:password]
              @user = User.find_by_email(params[:user][:email])
              if @user.valid_password?(params[:user][:password])
                @user.ensure_authentication_token!
                respond_to do |format|
                  format.json {
                    render :json => {
                        :success => true,
                        :user_id => @user.id,
                        :email => @user.email
                      }.to_json
                  }
                end
              else
                render :json => {:error => "Invalid login email/password.", :status => 401}.to_json
              end
            else
              render :json => {:error => "Please include email and password parameters.", :status => 401}.to_json
            end
          }
        end
      end
    

    Then on the iphone/objective-c side of things, you can use the ASIHTTPRequest library and JSONKit library:

    http://allseeing-i.com/ASIHTTPRequest/
    
    https://github.com/johnezang/JSONKit/
    

    Once you have all the aforementioned installed in xcode, then accessing the rails controller, getting the response as json, and handling it in objective-c is simple:

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/pages/login.json", RemoteUrl]];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request setRequestMethod:@"POST"];
    [request appendPostData:[[NSString stringWithFormat:@"{\"user\":{\"email\":\"%@\", \"password\":\"%@\"}}", self.emailField.text, self.passwordField.text] dataUsingEncoding:NSUTF8StringEncoding] ];
    [request startSynchronous];
    
    //start
    [self.loginIndicator startAnimating];
    
    //finish
     NSError *error = [request error];
    [self setLoginStatus:@"" isLoading:NO];
    
    if (error) {
        [self setLoginStatus:@"Error" isLoading:NO];
        [self showAlert:[error description]];
    } else {
        NSString *response = [request responseString];
    
        NSDictionary * resultsDictionary = [response objectFromJSONString];
    
    
        NSString * success = [resultsDictionary objectForKey:@"success"];
    
    
        if ([success boolValue]) {
            ....
    

    I just finished a rails/iphone application with tons of calls to Rails, so it’s definitely doable and a learning experience.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following the Ruby on Rails tutorial by Michael Hartl http://ruby.railstutorial.org/ I installed
I am practicing this RoR tutorial project of Michael Hartl: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book I am using
I have a basic authentication system just like in Michael Hartl's Ruby on Rails
I'm new to rails, and have taken some existing site for new enhancements. I
I have a Rails app which has 2 databases. Legacy DB with table called
So I have the foundation of my Rails app, then I went ahead and
I'm trying to learn both Ruby and Rails and I'm looking at Michael Hartl's
Im following the RoR tutorial by Michael Hartl and im at chapter 9.3 Showing
I have a simple Ruby on Rails form which includes an authenticity_token. Unfortunatly, I
In my Rails 3 ajax scaffolding I have a hook on ajax:success which takes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.