Rails link_to anchor with remote: true & method: :

2019-07-27 17:16发布

问题:

The anchor doesn't work with method: :post for some reason.

My view file index.haml:

/ some code here
%h3= Description
.results
  =render 'result_box'

_result_box.haml partial:

.row#scroll-here
  .col-md-12
    = link_to "Get result", get_result_path(@book, :anchor => "scroll-here", parameters: {first: 1, second: 2}), method: :post, remote: true

index.js.erb :

$('.results').html("<%= j (render partial: 'result_box') %>")

After pressing link_to and reloading ".results" the page focus is still on the top of the page. But I want it to be at the "#scroll-here".

回答1:

try this

 = link_to "Get result", get_result_path(@book, :anchor => "scroll-here", parameters: {first: 1, second: 2}), method: :post,{ remote: true}

or

= link_to "Get result", get_result_path(@book, :anchor => "scroll-here", parameters: {first: 1, second: 2}), method: :post,:remote=> true

Refer this link (http://haml.info/docs/yardoc/file.REFERENCE.html#inserting_ruby_)