1

I want to pass a ruby var as a javascript function param in a Rails checkbox, like this:

<%= check_box_tag 'Sugestão', prato.id , prato.sugestao,:class => prato.categoria_pratos_id, :id => "task-check3",:onchange =>"checkbox('<%=prato.categoria_pratos_id%>')"  %>

I have the Javascript checkbox Function working fine. But i need to pass the id as the param... It just gives me application error if i do that

3
  • u mean like an hidden field? Commented Jun 3, 2016 at 14:57
  • And by the way, you have erb interpolation in erb interpolation, while you just need "checkbox('#{prato.categoria_pratos_id}')" Commented Jun 3, 2016 at 14:59
  • works like a charm! Post the answer i'll accept :D Commented Jun 3, 2016 at 15:01

3 Answers 3

3

It's because what you're giving within <%= %> is Ruby. You don't have to use the notation again while passing your parameter. Simply use :onchange => "checkbox('#{your_id}')"

Sign up to request clarification or add additional context in comments.

2 Comments

what if i want to send 2 parameters?
"checkbox('#{param1}', '#{param2}')"
1

You have erb interpolation within erb interpolation, while you just need to interpolate ruby variable in a ruby string :

onchange: "checkbox('#{prato.categoria_pratos_id}')"

1 Comment

what if i want to send 2 parameters?
1

You can't use <%= %> within <%=%>. Instead, use #{} for the within. Like ...('#{prato.id}')...

2 Comments

how do i send 2 parameters?
The same: ('#{something}', '#{another}')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.