Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails - link_to same page but with anchor -> no reload

I want to use

link_to 'Cancel', edit_project_path(@project, url_options)

to cancel the edit and go back to the edit page.

I use jQuery UI Tabs for a tabbed edit page. Each tab has it's own form and submit/cancel buttons.

When I click a Cancel link I want to go back to the active tab. So I set

url_options = {:anchor => active_tab_id}

The problem is: the page doesn't reload because of the anchor. Adding data-no-turbolink does not help:

link_to 'Cancel', edit_project_path(@project, url_options), :data => {:no_turbolink => true}
like image 362
schoettl Avatar asked Jan 10 '15 17:01

schoettl


People also ask

How do I make a link not refresh the page?

If you put # inside the href like <a href="#"></a> then the link will not refresh or reload when clicked.

Does href reload the page?

window. location. href method returns/loads the current url. So we can use it to reload/refresh the page in javascript.

What is link_ to in Rails?

link_to is a Rails built in helper that helps generate an anchor tag.


2 Answers

try using
<%=link_to("Cancel", edit_project_path(@project, url_options), method: :get)%> this helped me better

like image 100
santhosh kumar Avatar answered Nov 15 '22 01:11

santhosh kumar


The only way to do it is with javascript.

Add custom data attribute to the link:

link_to 'Cancel', edit_project_path(@project, url_options), :data => { :reload => true }

Then put this javascript somewhere, for example in app/javascripts/reload_hash.js

    $(function(){
        $('a[data-reload="true"').on('click', function(e) {
            window.location = $(e.target).attr('href');
            window.location.reload(true);
        });
    });
like image 33
Alex Ponomarev Avatar answered Nov 15 '22 00:11

Alex Ponomarev