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
Related
For a testing helper I am working on I need to inject a route into the beginning of the routes. To add it at the end is easy:
test_routes = Proc.new do
get "/#{route_root}/:id", to: "#{route_root}#test"
end
Rails.application.routes.eval_block(test_routes)
The problem is that often there are "catch all" routes at the end of the application routes list, so I need to inject this as the FIRST route.
I have been using the technique here: How to dynamically add routes in Rails 3.2
but its hacky, and after a gem update last night it broke (not sure why yet, but its breaking inside of the routes.clear!) so I am looking for a less hacky solution.
Why not just create your own module like this:
module DynamicRouter
def self.routes router
router.get "/#{route_root}/:id", to: "#{route_root}#test"
end
end
and in your routes.rb just call your function:
# in routes.rb
# first line
DynamicRouter::routes(self)
That's how we do with some of our custom gems. You can also monkey patch the router, but it's more hacky.
I have an application with admin in ruby on rails. Now I need to add front-end in that application. But I don't know how ingrate with both in a single application.
You can create an "admin" area pretty simply once you know how. It all comes down to namespaces, specifically:
#config/routes.rb
namespace :admin do
# Sets up "/admin"
root "application#index"
end
Namespaces are essentially "folders", which also influence the names of your Rails classes (for example, your controller class names).
This means you'll be able to use the following:
#app/controllers/admin/application_controller.rb
class Admin::ApplicationController < ActionController::Base
layout :admin
def index
#do stuff here
end
end
Your models will remain as they are now (no need to make them admin namespaced).
--
The above code should give you the ability to access yoururl.com/admin and have a controller/action to work with. Of course, this negates the fact you're going to have to populate this area with data & controller actions; it all works much similarly to a "standard" rails app once you get it working.
You'll want to check out these helpful resources:
ActiveAdmin gem
RailsAdmin gem
RubyToolbox Admin gems
Railscasts Admin tutorial:
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
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.
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.