0

Update: Instead of

@company = Company.find(params[:company_id] in the Reimbursements New Action

I used @company = Company.find_by_name(params[:company_id]

This replaced the id in the URL with the name but still requires /reimbursements/new at the end of the URL. Any thoughts?

Overview: I have a Companies controller/model and a Reimbursements controller/model

A Company has_many reimbursements

A Company has a name as well

The Issue: When a new company is created the link to the new reimbursements form is

localhost:3000/companies/:id/reimbursements/new

This URL is very long and I would prefer it be:

localhost:3000/companies/company.name

Is this possible? How would I go about doing this? Thanks in advance!

szier
  • 1,427
  • 3
  • 19
  • 32
  • You can use whatever you want. Routes are just regular expressions. However the real question is if it is a good idea. Short url's are a marketing fad. – max Mar 18 '16 at 18:36
  • Can you please checkout the Update I posted? I would appreciate any insight – szier Mar 19 '16 at 20:45

2 Answers2

0

You might want to add a permalink field in Company Model.

Then use routes as

localhost:3000/companies/:permalink/reimbursements/new

Now in controller, find Company be permalink instead id Id.

Abhishek Patel
  • 138
  • 1
  • 6
0

I think it would be

get /companies/:name => reimbursements#new

Rails defaults to making the route to a certain controller action the first route to that action it sees so make sure you put this above any routes that might override it (i.e. any reimbursements routes you might have). As Max suggested though, this isn't great form and the companies id wouldn't be passed through params with this structure

SomeSchmo
  • 665
  • 3
  • 18
  • Your method did not work for me. Can you please checkout the update I posted? – szier Mar 19 '16 at 20:45
  • What about it didn't work? also is `Company.find_by_name(params[:company_id])` definitely working? It looks like you're telling AR to search for something by a name and giving it an id – SomeSchmo Mar 20 '16 at 18:34