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 8806977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:15:41+00:00 2026-06-14T02:15:41+00:00

My application currently handles HTML requests only and I would like to extend some

  • 0

My application currently handles HTML requests only and I would like to extend some actions to also handle JSON requests. Many of the controller actions perform redirects, set session values (flash) depending on whether the request was successful.

How can I incorporate responds_to to change action behavior based on request format for an action such as this:

 def create
  @user = User.new(params[:user])      
  if @user.save
    flash[:notice] = {:success => "User Created."}
    session[:user_id] = @user.id
        session[:username] = @user.username
    redirect_to(:controller => 'lists', :action => 'index')
  else
     flash[:notice] = {:error => @user.errors}
    render('new')
  end
end

So as you can see I am checking to see if the model is persisted and then either redirecting or rendering the form again. How can I maintain this current behavior but also handle requests in JSON? JSON requests will not need to perform redirects etc.

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-14T02:15:42+00:00Added an answer on June 14, 2026 at 2:15 am

    This seems to do the trick:

    def create
      @user = User.new(params[:user])      
      if @user.save
        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.html{render('new')}
          end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Log4Net to handle logging in my WPF application. Currently, the logger is
My Application currently serves requests for data, and can order the data to the
currently I am working on a spring based application. I do have some unit
i'm making some experiments with Jquery UI draggable, but currently my application is not
I'm currently developing a gem that handles mobile devices. The controller looks like this:
I have a GWT application which I would like to run from within a
We're struggling with a policy to correctly handle exceptions in our application. Here's our
Our application currently stores bunch of configuraton & application specific data to files on
So my iPhone application currently has a tabviewcontroller that populates the entire screen. The
I have an application (currently written in Python as we iron out the specifics

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.