Biju
Biju

Reputation: 53

Rails generate controller (under a namespace)

I want to add a controller and its routes entry under a namespace (api) for which I am proceeding with rails generate api/Users my_method, which creates the files and entries as follows:

  create  app/controllers/api/users_controller.rb
  route   get "users/my_method"
  invoke  erb
  create  app/views/api/users
  create  app/views/api/users/my_method.html.erb

Everything worked fine apart from the routes entry. What I am assuming is it should create the routes entry as well under the correct namespace or it shouldn't create it at all, or I am doing something wrong.On the other hand when going with scaffold way it does correctly.

Is it something which we need to do it manually?

Using ruby 2.0 and rails 4 for the application.

Upvotes: 4

Views: 4235

Answers (2)

Andrew Zhuk
Andrew Zhuk

Reputation: 500

Type in terminal

rails generate scaffold Api::User username email

rake db:migrate

this is part of result

class Admin::ServicesController < ApplicationController
# GET /api/users
# GET /api/users.json
def index
 @api_users = Api::User.all
end

To do generate and I think you will understand everything, don't forget see new app structure :-), Good luck, and solve your problem quickly as possible.

Upvotes: 4

Aaron Dufall
Aaron Dufall

Reputation: 1177

You can namespace your routes in your config/routes.rb like this

namespace :api do
  resources :user
end

Upvotes: 0

Related Questions