2

Ruby on Rails newbie question...

Consider the following code (which is within a view):

<div class="meta">
<%= link_to time_ago_in_words(status.created_at) + " ago", status %> |
<span class="admin">
<%= link_to "Edit", edit_status_path(status) %> |
<%= link_to "Delete", status, method: :delete, data: { confirm: "Ya sure?" } %>
</span>
</div>

Some of methods called within the ERB tags seem like magic to me, and I'm trying to demystify them. I don't feel comfortable using code if I don't understand how it works under the hood.

Can someone point me to where the following methods (er, I think they are methods, maybe that's not the right term) are defined and/or documented?

  • edit_status_path()
  • :delete
  • data:
  • confirm:
filmnut
  • 746
  • 1
  • 7
  • 16

2 Answers2

5

There is only one method here: edit_status_path. It is created within you routes file (config/routes.rb), most likely by another moethod called resources (in this case it would be resources :statuses).

:delete is not a method, it is a symbol: http://www.ruby-doc.org/core-2.1.2/Symbol.html

data: alone has no meaning in ruby, it always comes with a second part (like data: value). It is ruby syntax for Hash object: http://www.ruby-doc.org/core-2.1.2/Hash.html, using symbols for keys. So

{a: 1}

is the same as

{:a => 1}

I don't feel comfortable using code if I don't understand how it works under the hood. - I understand you completely. I strongly advise you to read some ruby book (not ruby on rails) before you start learning rails (and then start with a good book as well). The fact I started learning ruby from rails was the reason why I hated this language for couple of months before I fall for it.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • 1
    If reading a book sound boring, the [Codecademy Ruby Track](http://codecademy.com/tracks/ruby) is a nice interactive way to learn the basics. – Tamer Shlash Jun 03 '14 at 01:19
  • 1
    @BroiSatse: Thanks. I think your comment is moving me in the right direction. However, when I look in my routes.rb file, I only [this](https://gist.github.com/jonathancadepowers/76b6627bec6200d3df21). Unless I'm missing something here, I don't see anywhere that edit_status_path is defined. – filmnut Jun 03 '14 at 02:27
  • 1
    @BroiSatse: Nevermind. I found the answer to my question [here](http://api.rubyonrails.org/classes/ActionDispatch/Routing.html). "Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code." – filmnut Jun 03 '14 at 02:41
  • 1
    After more research, running `rake routes` helps you see exactly which routes are associated with your Rails project, including the ones that are **implied** (versus explicitly defined in a file within your project). – filmnut Jun 04 '14 at 02:17
1

ERB

ERB code is basically Ruby / Rails' way of dynamically handling code (like PHP's <$ $>). The use of ERB code is basically just about calling methods & classes which utilize the pre-processing power of Rails

To demystify your code, and as stated by @BroiState, you have several elements to consider:

--

Methods

edit_status_path(status)

This is a Rails method to render a path (which will basically output domain.com/path/to/your/controller

As mentioned, this will be derived from your config/routes.rb file. However, what I want to express the role methods play in your code. Every time you call a mysterious method in Rails, you're basically tapping into the MVC programming pattern - which means you're using the methods available to your view.

Every piece of ERB functionality is a method or object

In Rails' case, these are typically known as helper methods, which can both be explicitly declared (in app/helpers), or implicitly declared (inside Rails).

Many of the path helpers will be defined inside the Rails framework, which is why they seem mysterious. However, as you gain more experience, you'll see many different Rails Classes which define many of the mysterious methods - allowing you to gain a more intimate view of how this works

--

Arguments

The famous :symbol is a ruby module which basically allows you to define a temporary object, ready to be populated with data (I think)

All of the method: :delete etc - are all arguments for the methods you're using in your views. Every method you use has arguments, and many of them will be symbols.

ERB is basically just a case of using methods & objects, and your :arguments essentially allow you to use different methods in your views

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147