martinvigo
martinvigo

Reputation: 143

Handling different routes (with different params) for the same controller

PROBLEM DESCRIPTION:

I have a intermediate table X to map Elements from table A to elements from table B.

This table X contains also many columns regarding properties of this mapping. The uniqueness of a record in table X is not just the id od A and id od B but also a unique identifier that represents the mapping.

When trying to update a property of table X, I added to routes:

map.A :only=>[:none] do |a|
    a.resources B :controller=>X :only[:update]
end

map.resources x :only=>[:update]

So this basically gives me to possible urls to access the update method of my X controller.

QUESTION:

My question is: how can I tell when the controller was accessed by passing A and B or just X identifier? I know that looking at the params I can check if A is present and if not then it mus be X but this is not very practical/secure. If it gets more complicated I will need multiple ifs to detect the exact case.

Even worst, imagine that for some reason I have another route like:

map.resources Y :controller=>X :only[:update]

Then in my params hash I would just have :id so and if condition wouldn't even work...

COMMENTS:

I wish there was a :key option for the routes so you could rename the keys in the parameters hash but from what I have read in Rails 2 there is no such a thing and I would like to avoid plugins if possible. Isn't there a better way than if cases or parsing the url to tell what parameters where passed so I can look up my database?

Thanks!!!

Upvotes: 0

Views: 870

Answers (1)

corprew
corprew

Reputation: 2001

This seems like a case where you'd want to actually have two different actions handling the cases if it's that much of a concern, but you can hack around this by using the defaults hash -- http://guides.rubyonrails.org/routing.html#defining-defaults -- to specify a parameter that tells you which one you're following to get to the same case.

What I mean by two different actions is having an :action => update_from_a in the route and an appropriate method in the controller in addition to the normal :update method. Some of this advice may be rails 3-specific.

Upvotes: 1

Related Questions