Muhammad Raihan Muhaimin
Muhammad Raihan Muhaimin

Reputation: 5729

Understanding Haml Code

I am currently working with a RoR project where my team is using Haml as a templating engine. I have difficulty understanding the following code.

%td
   = link_to 'Edit', [:edit, :merchants, category, subcategory], class: 'btn'
   = link_to 'Delete', [:merchants, category, subcategory], :method => :delete, :data => { :confirm => 'Are you sure?' }, class: 'btn'

Which translate into html as

<a class="btn" href="/admin/categories/28/sub_categories/147/edit">Edit</a>
<a class="btn" data-confirm="Are you sure?" data-method="delete" href="/admin/categories/28/sub_categories/147" rel="nofollow">Delete</a>

From Haml documentation, I found out that [] notation used for object reference so I am not sure how the [ ] notation translated into href notation. I am pretty new to both Ruby On Rails and Haml, so any help will be appreciated.

Upvotes: 0

Views: 129

Answers (2)

nikolayp
nikolayp

Reputation: 17929

In Rails we have two ways to build pathes

Also some methods (form_for, link_to) not requires to call polymorpic_path and able to take array directly.

Upvotes: 0

Roman Kiselenko
Roman Kiselenko

Reputation: 44370

It nothing about the Haml. It is a Rails helper what's builds some_awesome_path from arrays of :symbol and variables.

Internally, Rails will convert [:edit, :merchants, category, subcategory] to a call to merchants_category_subcategory_path. Most (if not all) Rails methods that expect a path will also take an object representation of your resource, like respond_with, link_to, render, redirect_to, form_for, etc.

Some example:

This array:

[:new, @object, :post]

Translate to:

new_businesses_post_path() and new_businesses_post_url()

Creating URLs From Objects

Upvotes: 1

Related Questions