-1
<h1>Listing categories</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Thumburl</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @categories.each do |category| %>
      <tr>
        <td><%= category.name %></td>
        <td><%= category.thumburl %></td>
        <td><%= link_to 'Show', category_path(category) %></td>
        <td><%= link_to 'Edit', edit_category_path(category) %></td>
        <td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>


<%= link_to 'New Category', new_category_path %>

So, as I said in my last post, I'm trying to go through Ruby code as I find it on Codecademy's RoR tutorial. Since they have such a straightforward tutorial, I figure it would be ridiculous to not understand everything they throw at me.

In this bit, they have an html.erb file that is supposed to form a tabled-view. Everything seems clear to me until they get to <tbody>.

Here, first, we are iterating on an instance variable @categories...now, where this instance variable is found, I don't know. I thought instance variables meant they could only be used within a particular class, yet this is a markup page, i.e. no classes have been defined, so how does Ruby know where or what this instance variable is?

Next: so, as I understand it, the word 'category', here, is serving as a variable because it was specified as such by pipe syntax two lines earlier. Now, whether this variable can be used beyond this html.erb page is beyond me, cause I thought instance variables were denoted with an '@.' With this variable, we call the method 'name'. So, is 'name' a custom method, and if so, where is it defined?

Now, the third <td> tag contains code that tells Ruby to link to some name 'Show,' yet I'm not sure what this name points to...also, 'category_path' is a method we defined in our routes.rb file, and it has the parameter of our blocked variable 'category.'

Lastly, the final row cell links to some name 'Destroy,' but this comma syntax is something I haven't seen before:

<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
matt
  • 78,533
  • 8
  • 163
  • 197
Peter.Ward
  • 7
  • 1
  • 6
  • http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to – Joel Feb 19 '15 at 23:34
  • category is a variable containing an element in the @categories collection, which would have been created by the Categories Controller. It's 'existence' is for the duration of the each.do / end loop. Next iteration, it's a different element from the collection. Outside the loop, it doesn't exist. The category path helpers are using some Rails magic, where if you pass it the object, and the path needs an ID, it uses that object's ID attribute (category.id, not the object ID). – railsdog Feb 20 '15 at 00:34

1 Answers1

0

The instance variable @categories should be defined in CategoriesController, which is a subclass of action controller. Ruby's instance variables are not persistent, and any @variables you defined in a controller action is accessible in the action's view. I am not sure if you are familiar with MVC patterns, and if not, you can take a look at this question:

What is MVC in Ruby on Rails?

Name is an attribute of the resource/model category. You should see its class in app/model/category.rb. It is a subclass of ActiveRecord, which acts as a wrapper of the database of your choice. So you probably have a database table called categories with column name 'name'.

And as @JoelL mentioned, the link_to are view helpers that are meant to simplify your syntax in view files,make your code more organized, and save you time. You can define your own view helpers in app/helpers. Some of the helpers are automatically made for you when you define a resources :resource_name in config/routes.rb. In this case, you probably have resources :categories in your config/routes.rb

qubit
  • 769
  • 6
  • 18