Topic: Admin panel design question

I am starting out on my first rails app. I started by coding the first portion of the application which is the news. I want, ideally, the site to be setup so the user can go to "www..com/admin" and then select what he wants to change on any part of the site (not only news, so kind of like a main menu).

So anyway I coded the admin portion of the site for administrating the news. Well I coded it in the admin_controller.rb. Now I want to do a section that is for administering the "articles" portion of the site. My question is, I want this to be like www..com/admin/articles so should i use routes.rb or should I do some sort of sub-controller [if they exist]?

Lastly, if I do make a new controller it is going to be doing the same basic functions that the admin_controller.rb is going right now (CRUD stuff) and this doesn't feel like the DRY-est approach, meaning that I am repeating much of the same code.

I tried searching for an answer but those that were related were too complex for a new Rails programmer.

Re: Admin panel design question

I was going to reply with named routes, but the more that I think about it I would design your site first and then worry about the urls. It sounds like you have a good grasp so far of what you want. Maybe if you posted your models we could offer more advice. Someone more experienced than myself might be able to give a better answer, but like I said I find it works best if you think about your site in objects, relations, and actions while keeping CRUD and REST in mind and once you have it fully planned in your head (or on paper) then go into what each page/view will look like and then once the functionality is all coded refine urls and add stylings.

Good Luck and sorry if I didn't answer your question well.

Re: Admin panel design question

Thanks for the quick reply. My main concern is this scenario. I just setup the news section which takes advantages of tags [categories]. So I have a headlines [aka news] and a tags model.

The function of the site so far is this: You go to admin/new you post a new headline and select the tags you want [multiple if necessary, this is a many-to-many relationship].

This is all working perfectly fine up to this point.

Now I want to add a area in the admin panel that allows CRUD functionality to the tags themseleves (add new tags, delete tags, etc). From what most tutorials and things tell me, it would be best to create a new controller for this task? However coming from my JAVA / PHP background this does not seem like the DRY technique that Rails is so famous for. Normally in Java or PHP, I would use some sort of parameter that tells a universal update function what model to perform the action upon. I am pretty much just asking for an idea on which is the best route to go with rails. I am planning on this being a very large-scale app sometime in the future and I am trying to avoid blowing my leg off this early in the development process. smile

Re: Admin panel design question

anyone?

Re: Admin panel design question

I come from the same background and am having the same types of questions about rails.

Would love to see an answer to this.

"They say, Evil prevails when good men do nothing. What they should have said was: Evil Prevails."

http://www.vrazzle.com

Re: Admin panel design question

traviswblack wrote:

I come from the same background and am having the same types of questions about rails.

Would love to see an answer to this.

I am honestly surprised that no one has come into answer this question yet. This seems like and issue that numerous people are trying to come to grasps with when they make the switch to Ruby on Rails from other languages. If someone could take the time to answer it would be greatly appreciated.

Re: Admin panel design question

Hello I usually keep quite besides the general advice I gave before because there are much more experienced people on here than myself. One thing I think will help you get more responses is a more detailed description of the problem. I know it's probably straight forward and very clear to you but to someone that isn't looking at your code it may have several different interpretations.

It would help if you'd list your current controllers and models. I'm assuming you have a "headlines" and "tags" model and controller and maybe an admin/user model and controller. If so I'm not sure why you'd have a problem (especially if you used the scaffold_resource generater). Basically your admin will use the "headlines" controller to do CRUD for headlines and the "tags" controller to do CRUD for tags.

Now yes you may be doing the same CRUD operations on both "headlines" and "tags" and I can see why you might think this goes against DRY but I would say the MVC (model, view, controller) convention takes precedence here. Basically don't repeat yourself as you follow convention over configuration. Make sense? If not give us some more info on your setup.

On a side note if you don't have users but do need a simple admin functionality check out Ryan's Railscast #21. #19 and #20 may also give you some ideas.
Cheers,
Jack

Re: Admin panel design question

In the earlier days I was big on separating out the administration section, but with Rails I'm realizing it's much better to make the administration inline. As mentioned above, I touch on this in a series of Railscasts episodes. I recommend viewing them in order.

Creating separate CRUD actions for each model may seem like duplication, but this can be a little deceiving. There are often many exceptions hidden within which makes it easier to keep each controller separate. However, you may want to check out the make_resourceful plugin which does a good job of removing the duplication while still keeping it flexible.

Railscasts - Free Ruby on Rails Screencasts

Re: Admin panel design question

For small little controllers like your tags controller the DRYest I can get my controllers and still have them nice and legible is as follows:

this is an example of a controller that is used to add dating websites to populate a dropdown list (could very easily be compared to your situation with selectable tags

Model
[code=models/dating_website.rb]class DatingWebsite < ActiveRecord::Base
  validates_presence_of :name
  validates_uniqueness_of :name
end[/code]
Controller
[code=controllers/dating_websites_controller.rb]class DatingWebsitesController < ApplicationController
 
  def new
    if request.post?
      @dating_website = DatingWebsite.new(params[:dating_website])
     
      if @dating_website.save
        flash[:notice] = 'Dating website was successfully created.'
      end
    end
   
    redirect_to :action => 'list'
  end
 
  def list
   
  end
 
  def destroy
    DatingWebsite.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
 
end[/code]

View
[code=views/dating_websites/list.rhtml]<span>New Dating Website</span><br />
<%= error_messages_for 'dating_website' %>

<% form_tag :action => "new" do %>
    <%= text_field 'dating_website', 'name', :size => 20 %><br />
    <%= submit_tag 'Submit' %>
<% end %>

<span>Current Dating Website Options</span><br />
<table id='list' cellpadding='0' cellspacing='0'>
  <tr>
    <th>name</th>
    <th>actions</th>
  </tr>
 
<% DatingWebsite.find(:all).each do |r| %>
  <tr>
    <td><%= r.name %></td>
    <td>
        <%= link_to 'delete', { :action => 'destroy', :id => r }, :class => 'list', :confirm => 'Are you sure?', :method => :post %>
    </td>
  </tr>
<% end %>
</table>[/code]

So there is only one view which handles the creating,listing, and destroying of dating_websites.  Hope this helps some new comers.  One thing to note is that user of 'request.post?' in the controllers new method.  I found out about this a little late in the game since rail scaffolding automatically makes a new method for getting a new object, and then a create method for posting it.  I personally find it a bit bloated.  Any comments for improvement are welcomed.

"An egoless acceptance of stuckness is a key to an understanding of all Quality, in mechanical work as in other endeavors."
Zen and the Art of Motorcycle Maintenance