6

What is Wrong with this statement It is showing syntax error

<%= link_to image_tag('cancel.png'), {:action => 'remove', :id => question.id}, :title=>'Delete', :class=>'action', :onclick=>"removeQuestion("+ question.id +");return false;"%>

But

<%= link_to image_tag('cancel.png'), {:action => 'remove', :id => question.id}, :title=>'Delete', :class=>'action', :onclick=>"removeQuestion();return false;"%>

is correctly generating the below code

<a title="Delete" onclick="removeQuestion();return false" class="action remove" href="/quizzes/remove/1"><img src="/images/cancel.png?1290165811" alt="Cancel"></a>
3
  • Please show the syntax error that you're getting. Commented Nov 25, 2010 at 5:36
  • what is the syntax error message? Commented Nov 25, 2010 at 5:37
  • ALso please only tag the questions with the language, additional tags such as "helpers" / "link-to" are not helpful and are just tagspam :( Commented Nov 25, 2010 at 5:37

2 Answers 2

12

What you wrote

<%= link_to image_tag('cancel.png'), {:action => 'remove', :id => question.id}, :title=>'Delete', :class=>'action', :onclick=>"removeQuestion("+ question.id +");return false;"%>

This bombs because question.id is a Fixnum. You would get can't convert Fixnum into String TypeError.

Ways to solve this

<%= link_to image_tag('cancel.png'), {:action => 'remove', :id => question.id}, :title=>'Delete', :class=>'action', :onclick=>"removeQuestion("+ question.id.to_s +");return false;"%>

OR

<%= link_to image_tag('cancel.png'), {:action => 'remove', :id => question.id}, :title=>'Delete', :class=>'action', :onclick=>"removeQuestion('#{question.id}');return false;"%>

This will send the question id as a string to your removeQuestion javascript function.

OR

<%= link_to image_tag('cancel.png'), {:action => 'remove', :id => question.id}, :title=>'Delete', :class=>'action', :onclick=>"removeQuestion(#{question.id});return false;"%>

This will send the question id as an integer to your removeQuestion javascript function.

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

1 Comment

The last one will not send it as an integer but as an double, because JavaScript only knows double. Ok, it is compatible with 32bit integers, but still.
1
 <%= link_to image_tag('cancel.png'), {:action => 'remove', :id => question.id}, :title=>'Delete', :class=>'action', :onclick=>"removeQuestion($(this).attr('id'));return false;"%>

This will work

Comments

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.