简体   繁体   中英

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}

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

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);
        });
    });

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