I am using ruby on rails and I would like to render a partial on click (using jquery) an example of this being:
我正在使用ruby on rails,我想在单击(使用jquery)时呈现部分示例:
$('#submit').on('click', function(){
$('#lol').append("<%= escape_javascript(render :partial => 'myPartial' ) %>");
}
I basically want to submit a form then show the results (render partial) after in a read only format without the page refreshing. Problem is I can't use render in the asset pipeline. How do I include my JavaScript file from the view folder or what would be a good solution for this issue?
我基本上希望提交一个表单,然后以只读格式显示结果(呈现部分),而不刷新页面。问题是我不能在资产管道中使用渲染。如何从视图文件夹中包含JavaScript文件,或者如何解决这个问题?
1 个解决方案
#1
3
Looking at your code
看你的代码
$('#submit').on('click', function(){
$('#lol').append("<%= escape_javascript(render :partial => 'myPartial' ) %>");
}
I think you are confusing between ajax and normal jquery. If you don't need to perform anything else then you should use normal jquery to show and hide your content. For that you need to put this code in your app/assets/javascript/application.js or make a new file then require it inside application.js like
我认为您在ajax和普通jquery之间混淆了。如果您不需要执行任何其他操作,那么您应该使用正常的jquery来显示和隐藏您的内容。为此,您需要将这些代码放在应用程序/资产/javascript/应用程序中。js或制作一个新文件,然后在应用程序中要求它。js像
//= require file_name
Inside your view you can hide the parent element of your content by giving it a style like this:
在你的视图中,你可以隐藏你的内容的父元素,给它一个这样的样式:
#your_content_container{
display: none;
}
Then place your jquery code in your_file.js
然后将jquery代码放在_file.js中
$(document).on("click","#submit",function(){
$("##your_content_container").show();
})
If it's a form as your question says then you need to to perform some logic in your controller too so you should use Ajax
如果像你的问题说的那样它是一个表单,那么你需要在你的控制器中执行一些逻辑,所以你应该使用Ajax
Since it's a form so you need to use remote: true option in your form something like:
因为它是一个表单,所以您需要在表单中使用remote: true选项,比如:
<%= form_for(@user, remote: true) do |f| %>
// your fields
<% end %>
and then inside your controller you can have
然后在你的控制器里面
def your_method
#do your logic here
respond_to do |format|
format.js{}
end
end
This will allow rails to look for a file named your_method.js.erb in your views where you can call your jquery and render your partial. In your_method.js.erb
这将允许rails查找名为your_method.js的文件。在视图中的erb中,您可以调用jquery并呈现您的部分。在your_method.js.erb
$('#lol').append("<%=j render :partial => 'myPartial' %>");
For details refer to Working with Javascript in Rails
有关详细信息,请参阅在Rails中使用Javascript
#1
3
Looking at your code
看你的代码
$('#submit').on('click', function(){
$('#lol').append("<%= escape_javascript(render :partial => 'myPartial' ) %>");
}
I think you are confusing between ajax and normal jquery. If you don't need to perform anything else then you should use normal jquery to show and hide your content. For that you need to put this code in your app/assets/javascript/application.js or make a new file then require it inside application.js like
我认为您在ajax和普通jquery之间混淆了。如果您不需要执行任何其他操作,那么您应该使用正常的jquery来显示和隐藏您的内容。为此,您需要将这些代码放在应用程序/资产/javascript/应用程序中。js或制作一个新文件,然后在应用程序中要求它。js像
//= require file_name
Inside your view you can hide the parent element of your content by giving it a style like this:
在你的视图中,你可以隐藏你的内容的父元素,给它一个这样的样式:
#your_content_container{
display: none;
}
Then place your jquery code in your_file.js
然后将jquery代码放在_file.js中
$(document).on("click","#submit",function(){
$("##your_content_container").show();
})
If it's a form as your question says then you need to to perform some logic in your controller too so you should use Ajax
如果像你的问题说的那样它是一个表单,那么你需要在你的控制器中执行一些逻辑,所以你应该使用Ajax
Since it's a form so you need to use remote: true option in your form something like:
因为它是一个表单,所以您需要在表单中使用remote: true选项,比如:
<%= form_for(@user, remote: true) do |f| %>
// your fields
<% end %>
and then inside your controller you can have
然后在你的控制器里面
def your_method
#do your logic here
respond_to do |format|
format.js{}
end
end
This will allow rails to look for a file named your_method.js.erb in your views where you can call your jquery and render your partial. In your_method.js.erb
这将允许rails查找名为your_method.js的文件。在视图中的erb中,您可以调用jquery并呈现您的部分。在your_method.js.erb
$('#lol').append("<%=j render :partial => 'myPartial' %>");
For details refer to Working with Javascript in Rails
有关详细信息,请参阅在Rails中使用Javascript