[英]Rails link_to ruby variable in onclick javascript
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>
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. 这是炸弹,因为question.id
是一个Fixnum。 You would get can't convert Fixnum into String
TypeError
. 您将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. 这会将问题ID作为字符串发送到您的removeQuestion javascript函数。
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. 这会将问题ID作为整数发送到您的removeQuestion javascript函数。
<%= link_to image_tag('cancel.png'), {:action => 'remove', :id => question.id}, :title=>'Delete', :class=>'action', :onclick=>"removeQuestion($(this).attr('id'));return false;"%>
这会奏效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.