Rails: scaffold. Create admin controller for model - ruby-on-rails

I want to create admin interface for existing model User.
What is the simplest way?
I think, it's scaffolding.
Scaffolding in rails is generated by script(rails generate scaffold), and there is no options to do it dynamically (like in django), correct?
So how can I create scaffolding controller AdminUsers for model User? Create AdminUsers scaffolding and replace AdminUser model by User?
What should I do if model will be changed by migrations? Manually update scaffolding controller and views, right? Is there any automation?

You can generate the files dynamically using scaffolding- the scaffold command can take a namespaced argument.
rails generate scaffold Admin::User
You don't need another model though- your Admin::UsersController and views should be working with the User model.

Using standard solutions such as activeadmin or rails_admin is the simpliest way. If you want something custom and still wondering how to scaffold controller AdminUsers for model User, you might want to take a look at rails-admin-scaffold gem which automates this process and an article with more detailed explanation.

ActiveAdmin is a neat user interface for your Rails application. It gives you a fully customizable user interface to your models.
Site: http://activeadmin.info/
Demo: http://demo.activeadmin.info/admin
Railscasts Episode: http://railscasts.com/episodes/284-active-admin

Related

Generate form elements with scaffold_controller in Ruby on Rails

I have already a model in my Ruby on Rails aplication. I want to generate the Controller and Views.
Using the scaffold_controller option i get the files but the forms of the view dosnt contain the html input forms.
I can generate them using the parameters, for example
rails g scaffold_controller User name email
Is there some way to generate the html input tags without setting them as parameters? Can i make the scaffold_controller take the data from the existing model?
Thanks
Actually, when you use scaffold_controller, you are telling generator not to consider model. If you want to consider model attribites, you will need to pass the required attributes, as you are doing.
For more clarity:
Stubs out a scaffolded controller and its views. Pass the model name, either CamelCased or under_scored, and a list of views as arguments. The controller name is retrieved as a pluralized version of the model name.
To create a controller within a module, specify the model name as a
path like 'parent_module/controller_name'.
This generates a controller class in app/controllers and invokes
helper, template engine and test framework generators.
Another related post

How to create own generator in rails 4

I want to create own customized generator for scafflod so that i can change the default layout of generated .html.erb or .html.haml for a particular application.
I want to customize the layouts
Making generators is a good idea when you want a specific layout applied to few of your resources. It might be like you have an normal user section and admin user section with different layouts.
You may have a good reference of making a generator from here:http://railscasts.com/episodes/218-making-generators-in-rails-3?view=asciicast
There is some pretty nice stuff you can do with this. Problem is it is very badly documented, but once you know where to store the various template files, you're on the move:
It all starts in lib/templates
lib/templates/active_record/model/model.rb contains the model template
lib/templates/factory_girl/model/fixtures.erb contains the factory girl template
lib/templates/rails/scaffold_controller/controller.rb the controller
lib/templates/rspec/model/model_spec.rb the rspec model
lib/templates/rspec/scaffold/controller_spec.rb the rspec controller
lib/templates/haml/scaffold/_form.html.haml the views (idem dito for edit, index, new and show)
If you need more info on the templates, just let me know!

What is Scaffolding

I am having a question on scaffolding. Can someone explain what it does and how it works. I have search Google but I couldn't find anything that explained all the steps that happens.
A simple search in Google provides a lot of information. Also wikipedia
http://en.wikipedia.org/wiki/Scaffold_(programming)
Long story short a scaffold is simply a utility that most MVC web frameworks provide to create the necessary code/files for simple CRUD operations in the application.
In Rails, this means it will create the following from bottom up:
Active Record/Models
Migrations: These are used to create the necessary tables/columns for the model.
Models: Self explanatory, class of the model that subclasses from ActiveRecord::Base
Resource Routes
resources: :model: It generates the CRUD routes: index, show, new, create, edit, update, destroy by placing the resources: :model_name line in the routes.rb file.
ActionController
Controller: The controller that ties in the routes with the models and views with necessary code to perform the CRUD operations.
ActionView
Views: The views that display a very simplistic UI for performing the CRUD operations.
Assets: The javascripts, images, css, that are used in the views. This is very modular thanks to the assets pipeline.
It creates a bunch of other stuff based on your choices of test libraries. You can actually see what it's doing by just running rails scaffold SomeModel.
When I run the command:
rails generate scaffold peoples name:string age:integer
The following happens:
+ Rails connects to the database (defined in databases.yml) and creates a new table called peoples
+ In that table it creates two columns named name and age
+ Now it creates the web page that allows you to interface with the table
Scaffolding gives you a quick start on your Ruby on Rails projects.

Rails Create and Edit on the same view

I am trying to accomplish Create and Edit on the same view (and same controller) on Rails 3.2 and Ruby 1.9.2. I have a partial and I am using form_for and the standard form helpers.
I submit using Ajax, so my page (form) is not refreshed or redirected. When the user tries to edit the form, naturally, Rails will create an entry instead of updating it. I was thinking of modifying the Controller's create method to detect existing entries, but I am not sure if this is the correct approach. Thanks.
You can pretty easily include the same form in both create and edit pages by storing it in a partial (typically _form.html.erb) and rendering the form in both create and edit.
This is what rails generate scaffold MODEL FIELD:TYPE FIELD1:TYPE1 will give you, as well.

Rails Routes - How to add a route of an object without a controller

I created an object named settings. So i also provided its route in the routes.rb file i wrote "map.resources :settings". Now as i'm trying to save to the database with that object, it keeps on getting to the localhost:3000/settings url, which i don't have. i', also having this error
NameError in SettingsController#create
uninitialized constant SettingsController
PLEASE HELP! THANKS!
I am not 100% sure, but I believe you need to have a controller to add a route. Check out this diagram: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:mvc
If you use Rails, you have to stick to its rules. Rails implements the MVC pattern, where the controller has the role to provide the linking between a request started in the client web page (view) to creating, reading, updating and deleting (CRUD) objects (== models). The routes.rb define here the mapping from the URL to controller actions, not directly to the resources. See the "Rails Guides for Routing" for more information.
If you want to use your model objects, Rails provides an easy way to start that: scaffolding. By using rails generate scaffold setting <attr_name1>:<type1> ..., you are able to create the following:
A migration for the database that creates the settings table.
Generation of the model object Setting that maps to the created database table.
A controller SettingsController that allows CRUD for your model objects.
View files for the actions generated for the controller.
You can all do that by hand, but it is a good starting point to begin with. And read the basic tutorials and play with the example applications to get a feeling for Rails ...

Resources