0

I have a URL that looks like this:

http://localhost:3000/activities/5NcsdzWvXbv

I'd like to make it look something like this:

http://localhost:3000/activities/river-rafting

I have a column in my non-activerecord database that stores the names of activities that I'd like to use instead of the id.

I've taken a look at friendly_ID gem but it looks like it won't work for me because of the way my model is set up.

class Leads
    include ActiveAttr::Model
end

Is there an easy solution to this?

I checked out an old rails question that recommended doing this:

def to_param
  self.name
end

This wouldn't work for me as my model file isn't connected to activerecord but instead an external nosql database.

Can I just make some modifications to my routing file or is there some other way to modify my URL?

Thanks for your time.

LondonGuy
  • 10,778
  • 11
  • 79
  • 151

1 Answers1

0

I don't quite understand your problem.

If your routes.rb is set up properly, then it will translate to activities/:id. So, if you're preparing data in controller, then it's up to you how you will fetch the data, passing params[:id] to your non-activerecord ORM.

The :id is just a placeholder. If you defined your route as activities/:slug, then you would access the id part using params[:slug]

Can you please post the part of the code which is not working for you?

Edit:

You still haven't posted any code which is not working for you. Anyways, I feel that you need to check some docs on working with multiple databases in Rails. Here is a sketch for starters:

Class YourClass 
 establish_connection "your_external_database"
end

Then you use that connection to perform queries to the db, using your slugs.

Edit2:

Ahhh... you need to obfuscate ids. It just boils down to exposing encoded ids and then decoding them at your (backend) side :)

Here is an example approach: How do I obfuscate the ids of my records in rails?

Community
  • 1
  • 1
socjopata
  • 5,028
  • 3
  • 30
  • 33
  • It's not that it's not working. I don't understand how to setup my app so I use the name column of external database instead of the ID. – LondonGuy Oct 14 '14 at 13:52
  • Hi the database will remain the same. The activity will be searched for using the ID. What I need is to just disguise the ID. There is a way to do it as I've done it in the past but can't remember how exactly. – LondonGuy Oct 15 '14 at 22:56
  • It's similar to what I'd like to do. So for example I have an objectId: G5ryF5dg and that object has an activityType: Swimming. I need the objectId to find the correct object and this is why it's passed in the url. However I'd prefer the url to display the activity type but still search by my objectId. Is this possible? So search using objectId like I'm currently doing but display activityType value in URL instead of objectId – LondonGuy Oct 16 '14 at 11:28
  • As long as the object can be identified by the activityType (i.e, the activity type is unique for each object in the scope of objects) then it's possible. – socjopata Oct 16 '14 at 11:48
  • That's my problem. The activity type isn't unique. It can't be used to find the object. Is it possible to modify the code to include the activityType name as part of the url after the ID? Maybe obfuscate the Id then add activity type after it? – LondonGuy Oct 16 '14 at 12:07
  • Of course, it's pretty simple to do that. Remember, you control the `routes.rb`, thus you control what resources will be matched via given (custom) urls. – socjopata Oct 16 '14 at 13:47