views:

7950

answers:

4

I'd like to build a real quick and dirty administrative backend for a rails app I have been attached to at the last minute. I've looked at activescaffold and streamlined and think they are both very attractive and they should be simple to get running but I don't quite understand how to set up either one as a backend admin page. They seem designed to work like standard rails generators/scaffolds for creating visible front ends with model-view-controller-table name correspondence.

How do you create a admin_players interface when players is already in use and you want to avoid, as much as possible, affecting any of its related files?

The show,edit and index of the original resource are not usuable for the admin.

+3  A: 

I have used Streamlined pretty extensively.

To get Streamline working you create your own controllers - so you can actually run it completely apart from the rest of your application, and you can even run it in a separate 'admin' folder and namespace that can be secured with .

Here is the Customers controller from a recent app:

class CustomersController < ApplicationController
  layout 'streamlined'
  acts_as_streamlined       

  Streamlined.ui_for(Customer) do
    exporters :csv   
    new_submit_button :ajax => false 
    default_order_options :order => "created_at desc"   
    list_columns :name, :email, :mobile, :comments, :action_required_yes_no  
  end
end
Toby Hede
+25  A: 

I think namespaces is the solution to the problem you have here

map.namespace :admin do |admin|
    admin.resources :customers
end

Which will create routes admin_customers and new_admin_customers etc

Then inside the app/controller directory you can have an admin directory. Inside your admin directory create an admin controller ./script/generate rspec_controller admin/admin

class Admin::AdminController < ApplicationController

  layout "admin"
  before_filter :login_required
end

Then create an admin customers controller ./script/generate rspec_controller admin/customers

and make this inhert from your ApplicationController class Admin::CustomersController < Admin::AdminController

This will look for views in app/views/admin/customers and will expect a layout in app/views/layouts/admin.html.erb

You can then use whichever plugin or code you like to actually do your administration, streamline, ActiveScaffold, whatever personally I like to use resourcecs_controller, as it saves you a lot of time if you use a REST style architecture, and forcing yourself down that route can save a lot of time elsewhere. Though if you inherited the application thats a moot point by now

Laurie Young
a couple things you should add to this answer to help us rails n00bs: mention that the first code block is located in routes.rb and also mention that you need to install rspec and rspec-rails gems in order to generate an rspec_controller.
DJTripleThreat
A: 

Thanks Laurie. That mostly did the trick. I also had to create an admin directory in layouts so I could avoid name conflicts with the model's existing layout files.

srboisvert
I have edits my answer to include the normal expected location of the admin layout. I suspect that not many people will need to do it by an additional admin directory there :-)
Laurie Young
A: 

Hi Laurie,

I've tried to implement your idea but unfortunately it doesn't work. I'm using rails 2.2.2, created a subfolder called guru and updated the routes accordingly. unfortunately, i now get an error that says:

ActionController::RoutingError in Guru/articles#index

Showing vendor/plugins/streamlined/templates/generic_views/list.rhtml where line #3 raised:

No route matches {:action=>"list"}
Extracted source (around line #3):

1: <%= render_streamlined_file '/shared/_notice.rhtml' %>
2: <%= render_streamlined_file 'shared/_header_partials.rhtml' %>
3: <% form_for :page_options, @page_options, 
4:             :url => { :action => "list"}, :html=> {:id => :page_options, :style => 'display: none;'} do |form| %>
5:   <%# TODO: hide this form when JavaScript is available, then use it behind scenes%>
6:   Filter <%= form.text_field 'filter', {:autocomplete=>"off"} %>

In effect, looking at my routes:

$ rake routes | grep guru
                               guru_articles GET    /guru/articles                                                                              {:controller=>"guru/articles", :action=>"index"}
                     formatted_guru_articles GET    /guru/articles.:format                                                                      {:controller=>"guru/articles", :action=>"index"}
                                             POST   /guru/articles                                                                              {:controller=>"guru/articles", :action=>"create"}
                                             POST   /guru/articles.:format                                                                      {:controller=>"guru/articles", :action=>"create"}
                            new_guru_article GET    /guru/articles/new                                                                          {:controller=>"guru/articles", :action=>"new"}
                  formatted_new_guru_article GET    /guru/articles/new.:format                                                                  {:controller=>"guru/articles", :action=>"new"}
                           edit_guru_article GET    /guru/articles/:id/edit                                                                     {:controller=>"guru/articles", :action=>"edit"}
                 formatted_edit_guru_article GET    /guru/articles/:id/edit.:format                                                             {:controller=>"guru/articles", :action=>"edit"}
                                guru_article GET    /guru/articles/:id                                                                          {:controller=>"guru/articles", :action=>"show"}
                      formatted_guru_article GET    /guru/articles/:id.:format                                                                  {:controller=>"guru/articles", :action=>"show"}
                                             PUT    /guru/articles/:id                                                                          {:controller=>"guru/articles", :action=>"update"}
                                             PUT    /guru/articles/:id.:format                                                                  {:controller=>"guru/articles", :action=>"update"}
                                             DELETE /guru/articles/:id                                                                          {:controller=>"guru/articles", :action=>"destroy"}
                                             DELETE /guru/articles/:id.:format                                                                  {:controller=>"guru/articles", :action=>"destroy"}

Any ideas?

Laurie is more likely to notice if you comment on his response
srboisvert
This should have launched a different question instead of getting buried. You're not providing an answer, you're asking a question. Sorry to act like the SO police, but...
mannish