Run ruby ​​code in a link in Haml

I want to have the "delete user" link in a regular Activerecord table, but I cannot figure out how to enable inline ruby ​​in haml.

I have it:

%tbody - @users.each do |user| %tr %td= user.name %td= user.login %td %a %img{:src => '../images/delete.png', :title => 'Delete user'} 

How to do

 - user.destroy 

be a clickable link in Haml?

+4
source share
3 answers

I think you want

 %tbody - @users.each do |user| %tr %td= user.name %td= user.login %td = link_to image_tag('delete.png', :title => "Delete #{user}"), user_path(user), :method => :delete) 

See ActionView::Helpers::UrlHelper#link_to

Or if you are not using ActionPack ,

 %tbody - @users.each do |user| %tr %td= user.name %td= user.login %td %a{:href => "/users/#{user.id}?_method=delete"} %img{:src => '/images/delete.png', :title => "Delete #{user}"} 
+7
source

Here's a good tip on handling embedded Ruby in HAML. It even allows you to have punctuation marks (note the "!") After the links (this means that it is really built-in).

From the HAML FAQ :

If you paste something created by the helper as a link, then this is even simpler:

 %p== I like #{link_to 'chocolate', 'http://franschocolates.com'}! 
+2
source
 - @users.each do |user| = link_to user_path(user) do = image_tag 'delete.png', :title => 'Delete user' 
+1
source

Source: https://habr.com/ru/post/1306486/


All Articles