Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Clarification on rails view/controller interaction.

I'm working through the simple ruby on rails application video.

I have 1 'statuses_controller.rb' and 5 views inside a '/statuses/' directory; '_form.html.erb', 'edit.html.erb', 'index.html.erb', 'new.html.erb' and 'show.html.erb'.

Now, I assume rails works out the controller and the views are connected to each other due to the fact that the controller name is prefixed with the same directory name containing views (statuses).

The view, which I previously thought simply displayed data, actually interacts with application though the ruby code contained in its erb tags.

Here's the loop inside 'index.html.erb':

<% @statuses.each do |status| %>
  <tr>
    <td><%= status.name + '<br/>' %></td>
    <td><%= status.content %></td>
    <td><%= link_to 'Show', status %></td>
    <td><%= link_to 'Edit', edit_status_path(status) %></td>
    <td><%= link_to 'Destroy', status, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>

Inside my statuses_controller I have this method:

def index
@statuses = Status.all

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @statuses }
end
end

I assume all of the functionality inside 'index.html.erb' is provided by this index method, and rails knows to connect them because the view is called 'index.html.erb' and the method is called 'index'.

But I just can't get my head around what the index method is doing.

What does Status.all do?

What is the resond_to loop responding to? What is it putting inside the format variable? What does calling the html method on format do? And what about json?

The view makes more sense. I think the Status.all method returns an array of classes, the view's each loop then loops through all of the array classes, and returns information such as the name and content of each item.

I assume each status class looks something like this:

class status
    attr_accessor :id, :name, :content, :created_at, :updated_at
end

But then where does this class come from? That's what I don't get? Where? What file? Where do these attributes come from.

Also, the link_to method. It seems to take two parameters, a string, which it passes into the link html text, and a class object. My question is, does it automatically look inside for the class object for an ID attribute, and use that to append the url?

1 Answer

Whew, lots of questions. I'll try to answer as much as I can.

  1. Status.all is an ActiveRecord query that asks for "all" of the statuses from the database. It returns (spits out) an array of objects with all of data you see in the status class in the attr_accessor. That result is then assigned to the @statuses variable so it can be used elsewhere in your code efficiently.
  2. The index method is just making everything you need to pull the statuses into a view. It includes the @statuses instance variable to be used in the view and also the respond_to block that formats the data in a way that's usable by the view (format.html) and by JavaScript (format.json - json means JavaScript Object Notation and is specific object format for JS often used in APIs).
  3. The status class is a model which (usually) stores data in the database via ActiveRecord. Your status class is giving status 5 attributes (id, name, content, etc). It sits in a models directory adjacent to the controllers, views, etc directories.
  4. link_to is an ActionView UrlHelper. You're right about the string, but the second part is a path which is related to routes that are specified in the routes directory. More about Rails Routes

I hope that helps. The Rails Guides are really helpful.

Haha yes, a rambling question of mine. But thanks for the help and that link!