← Back to Upcase

Peculiar path helper behavior that I'm not familiar with


(Jonathan Garvin) #1

I’ve come across some code in a view that on includes a path helper in the format…

edit_company_url(:format => 'json')

What jumps out at me is that there’s no @company argument being passed to the helper. There is, however, a @company variable declared in the controller. When I load the page in a browser, the url is being correctly generated. I’ve never seen this before, but can helpers magically figure out the id without it being explicitly passed when an appropriately named instance variable is available?

I came across this code trying to write a view spec than renders this view, and the spec croaks on this exact helper call due to not being able to find the route without the ID, but I’ll save that part of the question for the TDD category once I’ve figured out why this works as written in the browser.


(Crispin Cornett) #2

I believe that Rails implicitly pulls this information out of the request parameters of the current page.

So if you’re on the ‘show’ page for the company e.g. 'http://localhost:3000/companies/1", the request parameters hash include:

{"action"=>"show", "controller"=>"companies", "id"=>"1"}

So just using edit_company_url or company_url will work if your current page’s request parameters include that information, but if you tried to use the code outside of that context, it would fail.

That said, it is strange and isn’t at all intuitive.


(Jonathan Garvin) #3

Thanks @crispincornett,

That’s what we started to figure it was, but it seems to then lead to some difficulty in testing the view with rspec, so I created a separate thread in the TDD section to see if anybody has any suggestions on that front.