<% tags.k...">

Rails link_to sends get instead of message

I have some links that I want to send a POST request, but they send get

<p class="tags">
  <% tags.keys.each do |key| %>
    <% if tags[key] == 1 %>
      <%= link_to by_tag_media_path(:tag => key), :remote => true, :method => :post do %>
        <span class="font-18"><%= key %></span>
      <% end %>
    <% elsif tags[key] == 2 %>
      <%= link_to by_tag_media_path(:tag => key), :remote => true, :method => :post do %>
        <span class="font-24"><%= key %></span>
      <% end %>
    <% elsif tags[key] == 3 %>
      <%= link_to by_tag_media_path(:tag => key), :remote => true, :method => :post do %>
        <span class="font-36"><%= key %></span>
      <% end %>
    <% end %>
  <% end %>
</p>

these links are in the partial name _tags.html.erb and on the index.html.erb page where there is a partial call. I have this js code:

function apply_filters(load_more){
          var params = "";

          $("a.selected").each(function(i, obj){
            params += $(this).text() + "/"
          });

          $("a.type_class").each(function(i, obj){
            params += $(this).text() + "/";
          });

          if(load_more){
            $.post("/media/filtered", {filters: params, load_more: true});
          } else {
            $.post("/media/filtered", {filters: params});
          }
        }

        $("#filtered_media_path").click(function(){
          apply_filters(true);
        });

when this js code is called I re-render the _tags.html.erb particle, and the links work fine (sends POST), but when the page is initially displayed, they send a GET request. The html code after rendering is the same in both cases.

Partial class index page:

<span id="cards">
    <%= render :partial => "media_cards", :locals => {:media_cards => @media_cards, :tags => @tags, :featured_cards => @featured_cards} %>
</span>

and particle media_cards:

 <div class="card media-card search hide-for-mobile">
    <input type="text" name="search-value" id="search_value"/>

    <span id="tags">
      <%= render :partial => "tags", :locals => {:tags => tags} %>
    </span>
  </div>

by_tags.js.erb:

$("#cards").html("<%= escape_javascript(render :partial => 'media_cards', :locals => {:media_cards => @media_cards, :tags => @tags, :featured_cards => @featured_cards}) %>");

the generated html looks like this:

<a href="/media/by_tag?tag=test" data-method="delete" data-remote="true" rel="nofollow">
        <span class="font-36">test</span>
</a>
+4
source share
2 answers

Here is the link to protype

link_to(body, url, html_options = {})

, , -

link_to("some text", by_tag_media_path(:tag => key),  :remote => true, :method => :post)
0

Source: https://habr.com/ru/post/1533516/


All Articles