Rails controllers names - ruby-on-rails

I'm new in Ruby on Rails world. I want to use singular controller name instead of plural.
Here is the url that I want to achieve.
http://foobar.com/admin/login
Here is what I try
rails g controller admin::login
When I try to access
http://foobar.com/admin/login
I get
uninitialized constant Admin::LoginsController
How to create singular controller instead of plural?

Whenever you go against Rails convention, the first thing you have to ask yourself is why are you doing it that way? If you are creating a login function, you should have an administrators/users controller and a sessions controller. You can still create the url /admin/login/ by using nested resources and custom routes, but have the logic in the sessions controller.
But if you really need to change that for some reason, you can do it using this type of technique: override default pluralize for model-name in rails3.

You should look at the rails namespaces first. For example:
namespace :admin do
resources :posts, :comments
match 'login' => 'controller#action'
end
It will produce routes with admin prefix.
So if you want singular controller you should dive deeper into inflectors. Check following links:
Pluralizations and Singularizations (Inflections) in Rails 3
ActiveSupport::Inflector
For example, add following lines to your config/initializers/inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable "login"
end
In general you should user user or session controller for login action, of course. Look at, for example, devise gem for more information and usage.

Related

proper way to define resources for singular name controller and model in rails?

if I have articles controller and article model I would create resources: articles in config/routes.rb
but let's say I have a singular controller that I want to use for control panel such as control_panel_controller.rb (not control_panels_controller.rb) and a model that matches that controller control_panel.rb Would I just use resources: control_panel as proper ruby convention or I would have to do it some other way?
You can specify singular routes and override the default controller name that is expected.
Here is how I set up my home page:
resource :home, only: [:show], controller: 'home'
class HomeController < ApplicationController
...
end
This gives a helper called 'home_path' that generates the '/home' URL.
Take a look at the Rails routing guide; it does cover these options they're just not super obvious.
Edit: btw, your model names are totally independent of the controllers/routes. Of course it's good convention to make them match where possible, but often times as your app grows there will be many models or service objects that don't directly correspond to a controller with CRUD actions for it.

Auto-generate routes for scaffolded controller in Rails 4?

I'm trying to get a quick-and-dirty Ajax UI going for an app that already has its data model well in hand - it's basically been managed via rails console so far. Anyway, I thought I would start by auto-generating the missing controller logic that you would get from a rails g scaffold, only instead with rails g scaffold_controller for an existing controller.
It created the controller, and the views, and the assets.. but it didn't touch the routes at all! It didn't even try, didn't say "warning: routes.rb has been modified, not changing" or anything like that, and there's no mention of routes at all in the help output of rails g scaffold_controller.
So how do I say "Just give me the normal routes you would have given me if I started from scratch, please!"?
If I understand the question:
Please, open the config/routes.rb file, and inside the block (routes.draw) add the resources method with the table name (plural of model) as param. Like this:
MyApp::Application.routes.draw do
resources :products
... # rest of code
end
That define the routes for RESTful actions over products. You can read more here
At the console you can run: rake routes to see the available routes at your app.
Although this is asking about Rails 4 for long time ago, but with Rails 5, rails g scaffold_controller still won't auto-generate route, I did it with below monkey patch:
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
patcher = Module.new do
extend ActiveSupport::Concern
included do
hook_for :resource_route, required: true
end
end
Rails::Generators::ScaffoldControllerGenerator.send :include, patcher

How to override spree users api

I have integrated spree with my application. I have overridden spree-user model to user model. I am trying to override api/users to come to my UsersController but instead everytime when I make a call to /api/users/, the control goes to Spree::Api::UsersController. I have the following route for user
namespace :api do
resources :users
end
But still my routes are overridden. What changes should I do to route /api/users/ to my local controller?
Try using Spree::Core::Engine.routes.prepend in routes.rb:
Spree::Core::Engine.routes.prepend do
namespace :api do
resources :users
end
end
Alternatively, try appending main_app when you call the route in your view code, eg:
main_app.api_users_path
Not sure if you have tried that already but hopefully it helps. You can read more at the SO threads below:
Adding Routes to Rails' Spree E-Commerce
How to add new view to Ruby on Rails Spree commerce app?
access rails url helper from deface

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.
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.
You can namespace your routes in your config/routes.rb like this
namespace :api do
resources :user
end

Rails Beginner - which controller to put functions in?

New to rails and I have what I think is a basic question.
In an admin view, there will be varying operations done on different data models. I have a layout "admin" which has various tabs the user clicks to load forms to edit various sets of data.
Should the controller for everything that can be edited in this view be in admin_controller (ie, have an edit_product, edit_user...), or is it better to leave the functions in the controller for each model (say users_controller, products_controller, orders_controller) and specify in the controllers to use the admin layout?
I'm working through my first rails project, and it seems either way works, but obviously I want to follow the right convention going forward so any hint, or a link to an article about this topic would be appreciated.
Thanks,
The proper Rails way to do this would be to use Namespaces. I'll give an example below:
Inside your controllers folder, you add a new folder called admin, and for each model you want to edit as an admin, add a controller. Here is a basic blog application:
app/
models/
views/
controllers/
users_controller.rb
posts_controller.rb
comments_controller.rb
admin/
users_controller.rb
posts_controller.rb
comments_controller.rb
Notice the new folder layer within our controller folder. Inside each of these files, you'll change the definition of the class, from:
class UsersController < ApplicationController
to:
class Admin::UsersController < ApplicationController
Now, within your congif/routes.rb file, you can namespace your routes to the admin namespace, like so:
map.namespace :admin do |admin|
admin.resources :users
admin.resources :posts
admin.resources :comments
end
Now, you can go to a URL such as: http://localhost:3000/admin/users/1 and you'll have access to whatever you specified in the admin version of your users controller.
You can read more in this StackOverflow question, and read up on the Routes here.
Good answer from Mike. I would add that you can see the "standard" rails code for this by using a generator:
# in rails 2.3
$ script/generate controller admin/users
# in rails 3.0
$ rails generate controller admin/users
The slash in the controller name defines a namespace. Also see rake routes for the named paths that it creates, e.g. admin_users_path etc.

Resources