Reputation: 541
I want to set a :data attr if a condition is met. In this case a user should have a role. So it's a boolean statement. Let me give an example.
- @data = 'contract-confirm'
.create_button= link_to 'Something', new_some_path(@customer), :class => 'btn btn-success', :'data' => @data ? 'contract' : nil
.clearer
So I know this might look strange but I want to set a data attribute if customer is labeled and then hook js to that data attr. That part works. What does not work is that now I'm setting the attribute always. So even in the case that customer does not have the role the js gets hooked. I know that I am not explicitly indicating at all the role here. I have a @customer.role? but I cant seem to incorporate it properply. I managed it before with an if else statement but then with a lot of duplication which I'm not so fund of. Any tips?
Upvotes: 2
Views: 1587
Reputation: 2796
You can try this piece of code:
link_to 'Something', ... , :data => @customer.role? ? 'contract' : nil
haml won't include nil attributes, so it should work as you expect.
Upvotes: 1
Reputation: 26
Try to replace :'data' => @data ? 'contract' : nil with :'data' => 'contract' if @data. I checked it and next code:
- @data_present = true
= link_to 'Something', root_path, :class => 'btn', :'data' => ('test' if @data_present)
renders to:
<a href="/" class="btn" data="test">Something</a>
And code without - @data_present = true renders to:
<a href="/" class="btn">Something</a>
Upvotes: 1