In my rails application I have dragdrop.js.erb in /assets/javascript folder. In which I have an ajax block which should render a partial in a div with an on click action. I tried many things but I can't get it working. Any help would be appreciated.
在我的rails应用程序中,我在/ assets / javascript文件夹中有dragdrop.js.erb。其中我有一个ajax块,它应该在div中使用on click动作呈现部分。我尝试过很多东西,但是我无法让它发挥作用。任何帮助,将不胜感激。
Here is the code:
这是代码:
$.ajax({
type: "GET",
url: "<%= Rails.application.routes.url_helpers.cookbooks_path %>",
success: function(resp){
$("#property").load("<%= escape_javascript (render :partial => 'recipes/package_form') %>")
}
2 个解决方案
#1
5
Asset Pipeline
资产管道
Firstly, you need to make sure that you don't use any Rails dynamic path helpers in your javascript directly.
首先,您需要确保不直接在javascript中使用任何Rails动态路径助手。
The issue is that if you pre-compile your asset pipeline, you'll typically find these dynamic path helpers won't work correctly. Although there's nothing "wrong" with it - I tend to keep Rails back-end code completely separate from the front-end, as to ensure the versatility of the application:
问题是,如果您预编译资产管道,您通常会发现这些动态路径帮助程序无法正常工作。尽管它没有“错误” - 我倾向于将Rails后端代码与前端完全分开,以确保应用程序的多功能性:
#app/assets/javascripts/application.js
$(document).on("click", ".element", function(){
$.ajax({
type: "GET",
url: "/cookbooks"
});
});
This will send a "naked" Ajax request (no params / body) to your controller backend:
这将向您的控制器后端发送“裸”Ajax请求(没有参数/正文):
#app/controllers/cookbooks_controller.rb
Class CookbooksController < ApplicationController
def index
@cookbooks = Cookbook.all
respond_to do |format|
format.html
format.js #-> loads /views/cookbooks/index.js.erb
end
end
end
The trick here is that you can now populate your index.js.erb
file with the params necessary to render the partial:
这里的技巧是你现在可以使用渲染部分所需的参数来填充index.js.erb文件:
#app/views/cookbooks/index.js.erb
$("#property").html("<%=j render :partial => 'recipes/package_form' %>")
--
-
This should work for you
这应该适合你
#2
6
Your problem is that you are trying to use render
in assets
, however, render
is not available in assets
您的问题是您尝试在资产中使用渲染,但渲染在资源中不可用
You should move your .js.erb
file to an appropriate directory under app/views
您应该将.js.erb文件移动到app / views下的相应目录中
Here are some other similar question:
这是一些其他类似的问题:
- Rendering A Partial In Assets
- 在资产中渲染部分
- Rails JS ERB File Cannot Find Method Render
- Rails JS ERB文件找不到方法渲染
- When rendering a partial from tasks.js.coffee.erb - undefined, render
- 从tasks.js.coffee.erb渲染部分时 - 未定义,渲染
#1
5
Asset Pipeline
资产管道
Firstly, you need to make sure that you don't use any Rails dynamic path helpers in your javascript directly.
首先,您需要确保不直接在javascript中使用任何Rails动态路径助手。
The issue is that if you pre-compile your asset pipeline, you'll typically find these dynamic path helpers won't work correctly. Although there's nothing "wrong" with it - I tend to keep Rails back-end code completely separate from the front-end, as to ensure the versatility of the application:
问题是,如果您预编译资产管道,您通常会发现这些动态路径帮助程序无法正常工作。尽管它没有“错误” - 我倾向于将Rails后端代码与前端完全分开,以确保应用程序的多功能性:
#app/assets/javascripts/application.js
$(document).on("click", ".element", function(){
$.ajax({
type: "GET",
url: "/cookbooks"
});
});
This will send a "naked" Ajax request (no params / body) to your controller backend:
这将向您的控制器后端发送“裸”Ajax请求(没有参数/正文):
#app/controllers/cookbooks_controller.rb
Class CookbooksController < ApplicationController
def index
@cookbooks = Cookbook.all
respond_to do |format|
format.html
format.js #-> loads /views/cookbooks/index.js.erb
end
end
end
The trick here is that you can now populate your index.js.erb
file with the params necessary to render the partial:
这里的技巧是你现在可以使用渲染部分所需的参数来填充index.js.erb文件:
#app/views/cookbooks/index.js.erb
$("#property").html("<%=j render :partial => 'recipes/package_form' %>")
--
-
This should work for you
这应该适合你
#2
6
Your problem is that you are trying to use render
in assets
, however, render
is not available in assets
您的问题是您尝试在资产中使用渲染,但渲染在资源中不可用
You should move your .js.erb
file to an appropriate directory under app/views
您应该将.js.erb文件移动到app / views下的相应目录中
Here are some other similar question:
这是一些其他类似的问题:
- Rendering A Partial In Assets
- 在资产中渲染部分
- Rails JS ERB File Cannot Find Method Render
- Rails JS ERB文件找不到方法渲染
- When rendering a partial from tasks.js.coffee.erb - undefined, render
- 从tasks.js.coffee.erb渲染部分时 - 未定义,渲染