Rails 3 Routes

Hi everyone,

I have a nested resource that is generating the route: /events/id/
registrations.json

However, I would like to know if I can do something like this: /events/
registrations.json

With that said I have three questions:
1) If I can do that, would the events id still be passed and give me
access to the proper registrations related to the event?
2) How does this look in my routes file?
3) Would there need to be a change in my controllers?

You may find the code I am working with at: github.com/TheCodeBoutique/
Nested-Resource-Demo

Kind Regards,
Chad Eubanks
The Code Boutique

Hi everyone,

I have a nested resource that is generating the route: /events/id/
registrations.json

However, I would like to know if I can do something like this: /events/
registrations.json

With that said I have three questions:
1) If I can do that, would the events id still be passed and give me
access to the proper registrations related to the event?

Well the event id has to be somewhere, if not in the path then as a query param or in post data - you can't get rid of it completely if you need to know which event's registrations should be fetched

Fred

Fred,

I understand I can not get rid of it completely. The reason I am asking this is this: I have a client side app that sets a proxy to the URL path with an extension .json.

The events id is not a constant integer and therefore I can not set the proxy URL to an explicit events id.

I need to have a constant URL path of events/registrations.json. From what you are saying. It sounds like it can be done but can you elaborate for me please.

.... This might be a better example: users/id/posts.json. When user one logs in the related posts are shown. When user two logs in the related posts are shown. However, the id can not be shown in the url.

Kind Regards,
Chad Eubanks

The ID has to be somewhere. So you could be getting it from the session, right?

Walter

Very true... The session controller would do such. But the question still lingers, does the id NEED to be in the url?

Kind Regards,
Chad Eubanks

Not if you're grabbing it from somewhere. The ID MUST be somewhere, or the find will fail.

@user = current_user

if you're using any sort of common authentication framework. From there, @user.posts is a drop-kick away.

Walter