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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:24:38+00:00 2026-06-14T09:24:38+00:00

I am currently working on a web application built on Rails 3 that heavily

  • 0

I am currently working on a web application built on Rails 3 that heavily uses Ajax/REST for the client side. Thus, I often find myself writing controller actions like this:

def create
  if !params[:name] 
    respond_to do |format|
      format.html { render json: {}, status: :not_found }
      format.json { render json: {}, status: :not_found }
    end
    return
  end

  account = ...
  respond_to do |format|
    format.html { render json: account }
    format.json { render json: account }
  end
end

Nearly all of my actions are returning a json object in a success case or an error code. However, I always have to write this verbose respond_to block and a return, if I want the action to return earlier.

Instead I would like to use something like this instead, or a similar alternative:

def create
  if !params[:name] 
    throw :not_found
  end

  account = ...
  return account
end

How can this be done with Rails 3+ ?

  • 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-14T09:24:39+00:00Added an answer on June 14, 2026 at 9:24 am

    Have a look into inherited_resources. This will allow you to rewrite your controller as:

    class SomeController < ApplicationController
      inherit_resources
      respond_to :html, :js, :json
    end
    

    That is it. All of your create/read/update/delete methods will be accessible as usual. You can, as I have in the past, inherit from a master resources controller which uses inherited_resources, and then you can tweak the responses in a more general way.

    class ResourcesController < ApplicationController
      inherit_resources
      respond_to :html, :js
    
      def create
        create! do |format|
          format.js do
            # generic code here for managing all create methods initiated via js
            # current model is avialbe via 'resource'
            # e.g 'resource.errors'
          end
        end
      end
    

    Then simply inherit from that controller:

    class SomeController < ResourcesController
    
    end
    

    This abstraction can be overkill for most purposes, but it has come in extremely handy when working 30 or 40 models which all require similar controllers.

    Inherited_resources offers many helpers for accessing the current model (referred to as resource) to facilitate dynamic references, so you can, for example, return relevant forms, or partials based on resource/model name.

    To give you an idea of how to use this, you could return forms for the current controller by using the controller name in the parameters. Should be noted that malformed controller names will not reach this method (as it will return 404), so it is safe to use:

      format.js do
        render "#{params[:controller]}/form"
      end
    

    Best of all, you can override any of the methods yourself by defining them in a particular controller.

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

Sidebar

Related Questions

I'm currently working a problem that requires my web application to generate a chart
I'm currently working on a (relatively) basic web application that functions as a time
I'm currently working on a web application that might need to push information to
I'm currently working on a web application that's sort of similar to Google Reader
I'm currently working on a concept for a heavy AJAX based web application. The
I'm currently working on web application using VB in ASP.NET. Right now I have
I am currently working on a web application. I was just wondering which has
I'm currently working on a web application development. Our company methodology is to work
I'm currently working on a simple web application where users each have their own
I'm currently working on an Asp.net MVC Web application. In my solution, there're two

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.