rails embedded ruby​​条件在点击事件上表现不正确

时间:2013-09-19 02:03:18

标签: javascript ruby-on-rails click conditional

我正在尝试从erb文件中的js脚本中调用函数(ajaxReq(val);)。我想仅在@years_true == true的情况下在click事件上调用该函数。当我做<%puts @years_true%>在click事件中,它返回正确的值。但是,当我将它包装在ruby if语句中时(因此它只在@years_true计算结果为true时调用该函数),即使@years_true为true,永远不会调用该函数。以下是我认为可能相关的所有代码,非常感谢。

UsersController:

@years_true = !@user.years.find_by_year(params[:age]).nil?

_showjs.html.erb:

$(".year").click(function(event) {
event.preventDefault();
var val = $(this).text();
$.get( "/<% if !@user.nil?%><%= @user.name %><% end %>/" + val, function(data) {
});
<% if @years_true %>
  ajaxReq(val);
<% end %>
history.replaceState({}, '', "/" + gon.username + '/' + val );
return false;
});

0 个答案:

没有答案