简体   繁体   English

重定向同一页面而不重新加载Rails

[英]Redirecting same page without reloading in rails

Redirecting same page without reloading whole page.I can use this javascript below 重定向同一页面而不重新加载整个页面。我可以在下面使用此javascript

javascript.rb javascript.rb

$("#myLink").click(function() {
    window.location.reload();
});

html.erb html.erb

<%= fields_for 'images[]', img do |p| %>
    <%= link_to img[:image_path],users_categorize_path(img),:html=>{:id=>"myLink"}%>
<% end %>

My problem is all are working properly but script cannot working. 我的问题是所有程序都正常运行,但是脚本无法运行。

How to include script id in rails link tag? 如何在Rails链接标签中包含脚本ID? or Have any other script for this? 或对此有其他脚本吗?

 <%= link_to img[:image_path],users_categorize_path(img), remote: true,:html=>{:id=>"myLink"}%>          

In run time the link will pass the params in controller and view page the params but image cannot display.Then i will remove the remote: true in view page it will working properly.what i can do? 在运行时,链接将通过控制器中的参数,并查看页面中的参数,但图像无法显示。然后我将删除遥控器:在视图页面中为true,它将正常工作。我该怎么办?

Try this: 尝试这个:

<%= link_to img[:image_path],users_categorize_path(img), remote: true,:html=>{:id=>"myLink"}%>

After this, make an ajax call to controlle's method in your js file. 之后,在您的js文件中对Controlle的方法进行ajax调用。 Then in respected method's js.erb refresh or reload code that you have written in your javascript file. 然后在受尊重的方法的js.erb中刷新或重新加载您在javascript文件中编写的代码。

You can try this 你可以试试这个

<%= link_to img[:image_path],users_categorize_path(img), remote: true, id: "myLink" %>

After that inspect this element in the browser to check id is applied to this element or not. 之后,在浏览器中检查此元素以检查是否将id应用于此元素。

Following link will describe how to apply id to a link_to element. 以下链接将描述如何将id应用于link_to元素。

Rails link_to assigning class and id Rails link_to分配类和ID

If the id is applied to the link correctly then, you need to bind the event like this. 如果将ID正确地应用于链接,则需要像这样绑定事件。

$("#myLink").on("ajax:beforeSend", function(){
   window.location.reload();
}).on("ajax:complete", function(){
  //preloader hide
});

Hope this help! 希望有帮助!

$("#myLink").click(function(e) {
    e.preventDefault();

    window.location.reload();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM