2

Learning Ruby and Rails. In following the getting started guide if I invoke

rails generate scaffold Post name:string title:string content:text

it generates among other things code like the following in index.html.erb:

<% @posts.each do |post| %>
  <tr>
    <td><%= post.name %></td>
    <td><%= post.title %></td>
    <td><%= post.content %></td>
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %></td>
  </tr>
<% end %>
</table>

My only concern in the above is edit_post_path, and my question is, what is it - and specifically from a Ruby standpoint. It certainly has every appearance of being a Ruby method, and its embedded in other code which is most definitely Ruby: posts.each do |post|...end that's all Ruby

But if edit_post_path is a Ruby method, where is the code for it? 'post' is a label I have provided to Rails, so presumably this Ruby method should be somewhere in my site directory along with other Ruby code generated when invoking "rails generate scaffold..." above (i.e. it wouldn't be in a Rails-specific directory for example). But there is no such method 'edit_post_path' defined anywhere. So is it not really Ruby at all, just something contrived to look that way for some reason, and really just a string of text that is processed by something strictly proprietary to Rails. Is this an example of what is so cool about Rails?

2 Answers 2

3

That is a Rails helper method - aka sugar syntax as James pointed out - for routing within your app.

To see all the routes available to you, at the command line do rake routes. You will see a list of the helpers on the left, then you will see the HTTP operation in the second column, third column = URL path format, and last column is a breakdown of the controller and action that it relates to.

To see some of the Ruby code that is at the heart of this magic, check it out in the Rails 3 repo, like this: https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/mapper.rb#L444

Also if you want to create custom URLs for specific resources, check out: https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/url_for.rb

Here is some more info on routing in general: http://guides.rubyonrails.org/routing.html

Hope that helps.

1
  • Beat me to it, good answer. OP, if you want to see where the helper methods (and yes, they are "straight Ruby methods," they're just defined dynamically when your app starts) are defined, take a look at this section: github.com/rails/rails/blob/… Commented Sep 24, 2011 at 4:38
2

It's "sugar"-syntax built into Rails. There are a ton of easy methods like this to speed up development.

4
  • Why couldn't they have made this a straight Ruby method just for the sake of uniformity. I can get my mind around this sort of anomaly.
    – Mark
    Commented Sep 24, 2011 at 3:51
  • Don't know. To me, Rails is great- it's made development a lot of fun for me.
    – jschorr
    Commented Sep 24, 2011 at 3:52
  • Guess the "convention over configuration" philosophy isn't for everyone :)
    – Jin
    Commented Sep 24, 2011 at 3:54
  • Jin - No, I actually do appreciate that - it seems that ROR is more consistent with that than say Django. Isn't there a principle though that if something looks like a duck and walks like a duck it should actually be a duck (and not something dressed up like a duck for no good reason).
    – Mark
    Commented Sep 24, 2011 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.