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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:34:40+00:00 2026-06-15T20:34:40+00:00

I have a rails site which is mainly REST based and I want to

  • 0

I have a rails site which is mainly REST based and I want to add JSON API support.

For a clean code base, should I add this support within the existing controllers or create new controllers which only handle this API methods and then move all the common code to models/helpers?

  • 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-15T20:34:41+00:00Added an answer on June 15, 2026 at 8:34 pm

    I used both techniques: writing API logic in the same controllers and making separate controllers for the API request.

    If it’s only an API, a small app used only by you, go with the default controller-model relation that rails offers. The code will be quite clean. Your routes file will be also clean.

    If you have a website and you want to build an API along, do it separately. I’ve build one alongside the existing controllers and the code was too messy. I refactored the code several times, but I still didn’t like it (it’s also a matter of personal taste).

    Another solution is to make controllers with a prefix. Ex: ApiUsersController. This would make your routes.rb file look ugly because you would have to manually specify the route to match the corresponding controller method.

    The working solution for me was to move all the API logic in separate controllers under an API namespace. Namespaces also let you do API versioning. So, for example, your routes will be:

    GET /api/v1/users.json
    POST /api/v1/users.json
    

    And then, in the future you can create another API version, let’s say v2, without breaking existing applications that use the older version of the API.

    You can find more about namespaces here: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

    An awesome tutorial about REST-full API with versioning: http://railscasts.com/episodes/350-rest-api-versioning?view=asciicast

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

Sidebar

Related Questions

I have a Rails application with a public REST API which authenticates API clients
I have a Ruby on Rails based site, in which I need to run
I have a site built in Ruby On Rails which has many ads in
I want to make a community based site, which is Drupal's strength. However I
I have a REST API which accepts XML in HTTP Posts. When I send
I have an existing Rails site which is rendering very poorly on iPhone and
I have Rails application (Redmine) and I have created next working code which close
I have a (Rails) site and I want the search engines to crawl and
I have a site, which has a jQuery based slider Awkward Showcase . In
I have a Rails site which uses the Delayed Job Gem to run a

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.