为什么这个控制器变量没有通过这个 javascript 代码?

Why is this controller variable not getting through to this javascript code?

我现在正在尝试使用我的 javascript 通过以下代码在单击元素时呈现新视图;

$(document).ready(function() {
    $('.link-panel').click( function() {
        window.location.replace('/quotes/'+gon.gon_quote_id);
    });
});

我收到以下错误:

Couldn't find Quote with 'id'=undefined [WHERE "quotes"."user_id" = ]

quote_controller.rb:

  def show
    @quote = current_user.quotes.find(params[:id])
    gon.gon_quote_id = @quote.id
  end

def index
@quotes = current_user.quotes.all
# how to pass the individual quote object's id to gon here

结束

我想这一定是我给 replace 方法的 url 参数的方式,你能帮我看看我做错了什么吗?

gon 警报测试表明设置工作正常。)

谢谢

您应该在 URL:

中使用 id 参数
window.location.replace('/quotes/?id=' + gon.gon_quote_id);

脚本不知道它要从 rails 访问变量,您必须这样做:

$(document).ready(function() {
    $('.link-panel').click( function() {
        window.location.replace('/quotes/'+<%= gon.gon_quote_id %>);
    });
});