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

  • Home
  • SEARCH
  • 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 8952365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:51:28+00:00 2026-06-15T13:51:28+00:00

I have a rails 3 controller which responds to both HTML and JSON client

  • 0

I have a rails 3 controller which responds to both HTML and JSON client requests. This controller has a ‘create’ method that creates a new ‘user’ model object with values posted in params[:user]. This works as intended when the user params values are posted from an HTML form. However when posting to this action from JSON client (iOS in my case) params[:user] is nil; I do notice that the params hash does contain the correct values if they are accessed directly i.e. params[:username] => “someusername”

Is there something I need to add to the HTTP request so I can use the params[:model_name] syntax in my controller? I am using ASIFormDataRequest.m to POST to my Rails application.

My client POST looks like this:

-(void)createUser:(NSString *)first
     lastName:(NSString *)last
     userName:(NSString *)user
        email:(NSString *)email
     password:(NSString *)password{

NSString *stringFormat = [NSString stringWithFormat:@"%@users.json", serviceBaseURL];
NSURL *url = [NSURL URLWithString:stringFormat];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
User *requestedUser = [[User alloc]initWithId:nil firstName:first lastName:last userName:user email:email password:password];
request.tag = CREATE_USER;
request.userInfo = [NSDictionary dictionaryWithObject:requestedUser forKey:@"requestedUser"];
[request setPostValue:first forKey:@"first_name"];
[request setPostValue:last forKey:@"last_name"];
[request setPostValue:user forKey:@"username"];
[request setPostValue:email forKey:@"email"];
[request setPostValue:password forKey:@"password"];
request.Delegate = self;
[request startAsynchronous];

}

The controller ‘create’ methods looks like so:

 def create
  @user = User.new(params[:user])      
  if @user.save  #This will fail because params[:user] is nil when posted from iOS!
    flash[:notice] = {:success => "User Created."}
    session[:user_id] = @user.id
        session[:username] = @user.username
         respond_to do |format|
            format.json {render :json => @user}
            format.html{redirect_to(:controller => 'lists', :action => 'index')}
         end

  else
     flash[:notice] = {:error => @user.errors}
      respond_to do |format|
         #format.json {render :json => @user.errors}
         format.json {render :json => params[:username]} #This has a value. So the form parameters are sent.
         format.html{render('new')}
      end
  end
end
  • 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-15T13:51:29+00:00Added an answer on June 15, 2026 at 1:51 pm

    The problem was I needed to POST my JSON in a format that RoR was expecting. The POST data was formatted like so:

    {first_name: "somename", last_name: "somelastname", username: "someusername", etc...}
    

    My RoR controller was expecting the attributes to be wrapped in a model object like so:

    {user:{first_name: "somename", last_name: "somelastname", username: "someusername", etc...}}
    

    Using ASIFormDataRequest this can be accomplished by formatting your post parameters like this:

    [request setPostValue:first forKey:@"user[first_name]"];
    [request setPostValue:last forKey:@"user[last_name]"];
    [request setPostValue:user forKey:@"user[username]"];
    [request setPostValue:email forKey:@"user[email]"];
    [request setPostValue:password forKey:@"user[password]"];
    

    Notice the ‘model_name[parameter]’ format.

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

Sidebar

Related Questions

I have a Rails controller action to test. In that action, a method User.can?
In my rails application, I have a controller called demo which is stored at
I have this command in a Rails controller open(source) { |s| content = s.read
I currently have the following controller method in a Rails app: def index @entries
I have a Rails 3 model D that belongs to another model, which belongs
OK, so I've built a Rails app in which I have some controller which
In a standard Rails controller, I'd create a record like this: @user = User.new(params[:user])
I have a standard controller which is set up to respond to HTML, JS
I am trying to create a simple rating controller based on this answer. Rails
I using the Rails resource_controller plugin. I have a ReleasesController, which is nested within

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.