Reputation: 3
I have a view that displays a list of items. Each item has several places where you can click to launch the same Bootstrap modal. I want to write a helper method to replace the redundant code, but I'm new to haml and can't get the helper method to work. I've tried to read the documentation on Haml::Helpers but am not clear about how everything fits together.
Redundant Code
{"data-target" => "#opportunityModal#{opportunity.id}",
"data-toggle" => "modal",
href: "#opportunityModal#{opportunity.id}"}
Redundant code wrapped in helper method
module OpportunitiesHelper
def modal_tag (type)
haml_tag type, {"data-target" => "#opportunityModal#{opportunity.id}", "data-toggle" => "modal", href: "#opportunityModal#{opportunity.id}"}
end
end
Snippet from current view
%li
.item.col-sm-4
%button{"data-target" => "#opportunityModal#{opportunity.id}",
"data-toggle" => "modal",
href: "#opportunityModal#{opportunity.id}"}
Quick View
.col-sm-8
%a{"data-target" => "#opportunityModal#{opportunity.id}",
"data-toggle" => "modal",
href: "#opportunityModal#{opportunity.id}"}
= opportunity.title
= render :partial => "opportunities/modal", :locals => { :opportunity => opportunity}
Same view using helper method
%li
.item.col-sm-4
-modal_tag(:button)
Quick View
.col-sm-8
-modal_tag(:a)
= opportunity.title
= render :partial => "opportunities/modal", :locals => { :opportunity => opportunity}
Resulting error when I try to use the helper method
syntax error, unexpected keyword_ensure, expecting end-of-input
Upvotes: 0
Views: 566
Reputation: 4171
You're definitely going to need a block.
- modal_helper(:a, opportunity.id) do
Quick View
I would do something like this:
def modal_helper(type, id, &block)
content_tag(type, href: "#opportunityModal#{id}", data: { target: "#opportunityModal#{id}", toggle: "modal" }) &block
end
That should get you pretty close. There might be some weirdness around blocks and outputting html. You might need html_safe, for instance.
Upvotes: 1