简体   繁体   中英

Redirecting same page without reloading in rails

Redirecting same page without reloading whole page.I can use this javascript below

javascript.rb

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

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? 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?

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. Then in respected method's js.erb refresh or reload code that you have written in your javascript file.

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.

Following link will describe how to apply id to a link_to element.

Rails link_to assigning class and id

If the id is applied to the link correctly then, you need to bind the event like this.

$("#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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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