1

Iam working with Rails from nearly 1 year and as a learner the most difficult matter to me is the view stuff.

I have view file that embeds this: <%= index %>. I tried to find where this method is defined in the code. I searched and found 12 definitions for index but non of them was in a helper. I googled it (sometimes strange names like options_from_collection_for_select OMG :<> appear to be part of the ActionView library) i didnt find it in the action view stuff either.

So I have had this problem multiple times and couldn't find out how the view automagically find this method.

Could you please guide me where to search?

Hairi
  • 3,318
  • 2
  • 29
  • 68

2 Answers2

1

You should use the ruby method source_location to know where a method is defined.

But you need to execute it in a context where you have access to it. So, if it's helper, then you can, in a rails console, try something like helper.method(:index).source_location

If it's not a helper (as it doesn't seem to be), then try to add a breaking point for example, and run method(:index).source_location, you should be able to locate it

But, are you sure that it's not a local variable, given when calling a partial, or from a each_with_index block ? It's really strange to name a method index without more context :/

kevcha
  • 1,002
  • 9
  • 15
0

One easy way to know where and how a method is defined is to use pry-byebug's binding.pry.

You can put <% binding.pry %> in your view file, reload the page, and then see the source of the desired method by entering show-source method_name in the command prompt that has appeared in your Rails server output. (show-source index in your case.)

hmnhf
  • 423
  • 3
  • 7