Restful Nested Resource without ugly id

To all:

hi i am pretty noob. Basically I am learning via copy and paste, so
please forgive me.

I am using rails 1.2.6.

I built 2 scaffold_resource called traders and orders.

I want a localhost:3000/v1/traders/{trader_name}/orders

so I did a

   base = '/v1'

  map.resources :traders, :path_prefix => base

  #trader_url
  trader_url = base + '/traders/:login'

  map.resources :orders, :path_prefix => trader_url

inside the orders_controller:

i did this
def index

    @orders = Order.find_by_trader_id(@trader.id)
    respond_to do |format|

       format.html
    end

I have 2 questions:

1) am i doing it right in the routes.rb?

2) i get an error at the webpage.

undefined method `each' for #<Order:0x4624f18>

Extracted source (around line #7):

4: <tr>
5: </tr>
6:
7: <% for order in @orders %>
8:
9: <tr>
10: <% for column in Order.content_columns %>

what is wrong?

Please help. I thank you all.

Thank you.

However, is that a good idea about using the @trader.orders.each do |
order>
considering that it appears less MVC?

What about my routes.rb? Any room for improvement?

map.resources :traders do |trader|
  trader.resources :orders
end

would give you nice path helpers like:

trader_orders_path(trader_id)

i googled around and i know abt the map.resources code block that you
showed me.

however, that would give me urls like /traders/2/orders/2

instead of traders/peter/orders/2

is there an alternative to the one that I wrote and still give me what
i want?

thank you.

In order to do what you want to do you will have to do two things:

1, override the to_param method on your models

Assuming you have a “name” field in your Trader model, in the Trader.rb file, you’d add this:

def to_param
self.name
end

That’ll make your URLs work as long as you’re always passing the @trader object around. However, you have to change all of your finders now to look up by name

So, instead of

@trader = Trader.find(params[:id])

You must do
@trader = Trader.find_by_name(params[:id])

Repeat for the user model and controllers.

If you’re just starting out, don’t mess with this now. Learn how routing and finders actually work. You may discover a better way too. The ids don’t hurt anything and are much less volatile. If you change the trader’s name for example, the link is no longer valid.

Also, scaffolding isn’t meant for production. The URL stuff you’re trying to do is infinitely easier to do if you don’t use scaffolding. That’s why I say don’t tackle this now, wait until you understand how everything works together. Once you get it, these types of URLs become trivial because you’re WRITING the finders yourself, instead of changing a bunch that were originally intended to work a different way.

Best of luck!

Learn how routing and
finders actually work.

I am currently learning by playing around with the scaffolds. Do you
have certain resources to point me to? Bear in mind, I am pretty damn
noob.

Also, scaffolding isn't meant for production.

Oh no, I want to do one for a very simple game for production. If I
dont use scaffold, i may be lost. Even if i code from scratch, i will
inevitably copy teh code from scaffold.

I do have the eBook of Agile Devt with Rails and a few other Rails
eBook. But sometimes I feel the examples they use are a tad too
simple. Furthermore, i intend to use make a more Restful application.

Please help. THanks