10

I have a link_to on my rails4 page which uses slim syntax. The following link_to

link_to exports_path, data: { confirm: "Are you sure?" }

is now required to only show the confirm message upon a certain condition. How do we make this happen in rails4?

I tried:

link_to exports_path, data: { confirm: result_count > 50 ? "Are you sure?" : nil }

which seems to always show the confirm regardless of the condition..

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
Swaroop
  • 431
  • 5
  • 15

2 Answers2

11

How about

link_to exports_path, data: (result_count > 50  ?  { confirm:  "Are you sure?"} : nil)
DeeY
  • 922
  • 1
  • 8
  • 15
  • Works perfectly for the one data attribute. Thanks a ton!How would this scale if there were more attributes? – Swaroop Dec 11 '13 at 07:43
  • data: takes a hash of any size, like {confirm: 'really?', color: 'red', menu: 'next'} and so on – DeeY Dec 11 '13 at 07:46
  • What if I want the conditional to execute on the client-side? Any clean solution for that scenario? – elsurudo Feb 04 '15 at 14:17
0

you can also have the conditional for setting the confirm message or nil inside the data hash itself:

link_to exports_path, data: { some_data: "value", confirm: result_count > 50 ? "Are you sure?" : nil }

VinnyQ77
  • 385
  • 2
  • 10