gon未在javascript中定义错误

时间:2021-11-27 08:59:51

pretty simple question. I'm trying to use gon. I set the variable in the controller like this:

非常简单的问题。我正在尝试使用gon。我在控制器中设置变量,如下所示:

gon.preference = "hello"

and in my .js.erb file, I try to use the variable like this:

在我的.js.erb文件中,我尝试使用这样的变量:

console.log(gon.preference)

but I get an error saying that "gon is not defined". What could be the problem? Obviously rails recognizes that there is a gon variable. the .js.erb file is in my assets/javascripts directory. I tried changing the file name to .js (though I didn't expect this to make a difference at all). obviously no change.

但我得到一个错误,说“gon没有定义”。可能是什么问题呢?显然,rails认识到有一个gon变量。 .js.erb文件位于我的assets / javascripts目录中。我尝试将文件名更改为.js(虽然我没想到这会产生任何影响)。显然没有变化。

No clue why gon is just not working!

不知道为什么gon不起作用!

Help?

帮帮我?

9 个解决方案

#1


12  

Good answers here but figured out what was going on exactly. I was rendering a particular view without layout multiple times. However, since I was not rendering the layout while re-rendering this view, the <%= include_gon %> line in application.html.erb was useless.

这里很好的答案,但确切地知道发生了什么。我多次渲染没有布局的特定视图。但是,由于我在重新渲染此视图时没有渲染布局,因此application.html.erb中的<%= include_gon%>行无用。

I put <%= include_gon %> directly into the view I'm rendering, and everything works fine now!

我将<%= include_gon%>直接放入我正在渲染的视图中,现在一切正常!

#2


4  

I found adding this to your header helps.

我发现将此添加到您的标题有帮助。

<%= include_gon(:init => true) %>

Works great for rails 4 as well.

也适用于导轨4。

#3


2  

I created a test project to attempt to see if I have the same problem, Which I DID!

我创建了一个测试项目,试图看看我是否有同样的问题,我发现了!

Solution add the tag <%= include_gon %> in your header within application.html.erb:

解决方案在application.html.erb中的标头中添加标记<%= include_gon%>:

app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Test</title>
  <%= include_gon %>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>

In the view:

在视图中:

console.log(gon.preference)

In the controller:

在控制器中:

gon.preference = "hello"

Output: gon未在javascript中定义错误

输出:


Read more here

在这里阅读更多

#4


1  

An alternative to @Ringo's solution would be to leave the statement in the application layout file, simply modifying it as follows:

@ Ringo解决方案的替代方法是将语句保留在应用程序布局文件中,只需按如下方式修改它:

<%= include_gon if defined? gon && gon.present? %>

This keeps the include_gon reference as DRY as possible while still being as flexible and unobtrusive.

这使得include_gon引用尽可能保持DRY,同时仍然保持灵活和不引人注目。

#5


1  

I had the same error message and also used user456584's answer but the error message remained. Once I added gon.event = @event or gon.events = @events as below in the controller, I guess gon became defined and therefore, I no longer had the error message "gon is not defined".

我有相同的错误消息,并使用user456584的答案,但错误消息仍然存在。一旦我在控制器中添加gon.event = @event或gon.events = @events,我猜gon已经定义,因此,我不再有错误消息“gon未定义”。

def show
    @event = Event.find(params[:id])
    gon.events = @events
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @event }
    end
end

I am a new user and I was only trying to contribute. This was the only method that worked for me after all other suggestions.

我是新用户,我只是想贡献。在所有其他建议之后,这是唯一对我有用的方法。

#6


1  

I just ran across what I think is the same issue myself, and I'm wondering if the include_gon call should be in the body rather than the header of the application.html.erb. Putting a js alert in both the header and body of application.html.erb shows that the header is not always invoked, while the body is.

我只是遇到了我认为同样的问题,我想知道include_gon调用是否应该在正文中而不是application.html.erb的标题中。在application.html.erb的标题和正文中都放置了一个js警报,表明标题并不总是被调用,而正文是。

I was experiencing a lack of gon if I initially loaded a page on my site other than the one that used it, and then navigated there via header links, but if I initially loaded the gon-using page, the site would work fine until a reload on any other page.

如果我最初在我的网站上加载了一个页面,而不是使用它的那个页面,然后通过标题链接在那里导航,我遇到了缺乏gon但是如果我最初加载了gon-using页面,该网站将正常工作,直到在任何其他页面上重新加载。

Moving include_gon from the header to the body of the layout seems to have fixed this. I realize this is not how the gon docs suggest this should be done, but I'm not understanding how it would work the other way.

将include_gon从标题移动到布局的主体似乎已经解决了这个问题。我意识到这不是gon docs如何建议应该这样做的,但是我不知道它将如何以另一种方式工作。

#7


1  

Here is what happened to me

这是发生在我身上的事

The <% include_gon %> was in the application.html.erb but was still getting this error.

<%include_gon%>位于application.html.erb中,但仍然出现此错误。

The final reason was that in the controller the name of the method were I set the gon variables did not match the route of view. So the controller method was not being called before rendering the view and it seems that gon is only included when is used (which is great)

最后的原因是在控制器中,方法的名称是我设置的gon变量与视图路径不匹配。因此在渲染视图之前没有调用控制器方法,并且似乎只在使用时包含gon(这很棒)

To solve it, in my case I just needed to make sure that the routes.rb route and the name of the Controler/Method matched

要解决它,在我的情况下,我只需要确保routes.rb路由和Controler / Method的名称匹配

#8


0  

You should add

你应该添加

<%= Gon::Base.render_data %>

to the head section of your application.html.erb

到application.html.erb的head部分

#9


0  

I've got the same problem recently and my solution was adding

我最近遇到了同样的问题,我的解决方案是添加

if(window.gon != null) {
  ...
}

in my javascript file

在我的javascript文件中

#1


12  

Good answers here but figured out what was going on exactly. I was rendering a particular view without layout multiple times. However, since I was not rendering the layout while re-rendering this view, the <%= include_gon %> line in application.html.erb was useless.

这里很好的答案,但确切地知道发生了什么。我多次渲染没有布局的特定视图。但是,由于我在重新渲染此视图时没有渲染布局,因此application.html.erb中的<%= include_gon%>行无用。

I put <%= include_gon %> directly into the view I'm rendering, and everything works fine now!

我将<%= include_gon%>直接放入我正在渲染的视图中,现在一切正常!

#2


4  

I found adding this to your header helps.

我发现将此添加到您的标题有帮助。

<%= include_gon(:init => true) %>

Works great for rails 4 as well.

也适用于导轨4。

#3


2  

I created a test project to attempt to see if I have the same problem, Which I DID!

我创建了一个测试项目,试图看看我是否有同样的问题,我发现了!

Solution add the tag <%= include_gon %> in your header within application.html.erb:

解决方案在application.html.erb中的标头中添加标记<%= include_gon%>:

app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Test</title>
  <%= include_gon %>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>

In the view:

在视图中:

console.log(gon.preference)

In the controller:

在控制器中:

gon.preference = "hello"

Output: gon未在javascript中定义错误

输出:


Read more here

在这里阅读更多

#4


1  

An alternative to @Ringo's solution would be to leave the statement in the application layout file, simply modifying it as follows:

@ Ringo解决方案的替代方法是将语句保留在应用程序布局文件中,只需按如下方式修改它:

<%= include_gon if defined? gon && gon.present? %>

This keeps the include_gon reference as DRY as possible while still being as flexible and unobtrusive.

这使得include_gon引用尽可能保持DRY,同时仍然保持灵活和不引人注目。

#5


1  

I had the same error message and also used user456584's answer but the error message remained. Once I added gon.event = @event or gon.events = @events as below in the controller, I guess gon became defined and therefore, I no longer had the error message "gon is not defined".

我有相同的错误消息,并使用user456584的答案,但错误消息仍然存在。一旦我在控制器中添加gon.event = @event或gon.events = @events,我猜gon已经定义,因此,我不再有错误消息“gon未定义”。

def show
    @event = Event.find(params[:id])
    gon.events = @events
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @event }
    end
end

I am a new user and I was only trying to contribute. This was the only method that worked for me after all other suggestions.

我是新用户,我只是想贡献。在所有其他建议之后,这是唯一对我有用的方法。

#6


1  

I just ran across what I think is the same issue myself, and I'm wondering if the include_gon call should be in the body rather than the header of the application.html.erb. Putting a js alert in both the header and body of application.html.erb shows that the header is not always invoked, while the body is.

我只是遇到了我认为同样的问题,我想知道include_gon调用是否应该在正文中而不是application.html.erb的标题中。在application.html.erb的标题和正文中都放置了一个js警报,表明标题并不总是被调用,而正文是。

I was experiencing a lack of gon if I initially loaded a page on my site other than the one that used it, and then navigated there via header links, but if I initially loaded the gon-using page, the site would work fine until a reload on any other page.

如果我最初在我的网站上加载了一个页面,而不是使用它的那个页面,然后通过标题链接在那里导航,我遇到了缺乏gon但是如果我最初加载了gon-using页面,该网站将正常工作,直到在任何其他页面上重新加载。

Moving include_gon from the header to the body of the layout seems to have fixed this. I realize this is not how the gon docs suggest this should be done, but I'm not understanding how it would work the other way.

将include_gon从标题移动到布局的主体似乎已经解决了这个问题。我意识到这不是gon docs如何建议应该这样做的,但是我不知道它将如何以另一种方式工作。

#7


1  

Here is what happened to me

这是发生在我身上的事

The <% include_gon %> was in the application.html.erb but was still getting this error.

<%include_gon%>位于application.html.erb中,但仍然出现此错误。

The final reason was that in the controller the name of the method were I set the gon variables did not match the route of view. So the controller method was not being called before rendering the view and it seems that gon is only included when is used (which is great)

最后的原因是在控制器中,方法的名称是我设置的gon变量与视图路径不匹配。因此在渲染视图之前没有调用控制器方法,并且似乎只在使用时包含gon(这很棒)

To solve it, in my case I just needed to make sure that the routes.rb route and the name of the Controler/Method matched

要解决它,在我的情况下,我只需要确保routes.rb路由和Controler / Method的名称匹配

#8


0  

You should add

你应该添加

<%= Gon::Base.render_data %>

to the head section of your application.html.erb

到application.html.erb的head部分

#9


0  

I've got the same problem recently and my solution was adding

我最近遇到了同样的问题,我的解决方案是添加

if(window.gon != null) {
  ...
}

in my javascript file

在我的javascript文件中