通过AJAX更改link_to参数

changing link_to parameters via AJAX

本文关键字:to 参数 link AJAX 更改 通过      更新时间:2023-09-26

我得到了这个链接

<%= link_to "show replies", tweets_show_replies_path(:parent => tweet, active: false ), method: :post, remote: true, class: "show-replies" %>

和这个函数在我的"tweets"控制器

def show_replies
    @parent = Tweet.find(params[:parent])
    @tweet = Tweet.new
    respond_to do |format|
        format.html {render :nothing => true}
        format.js
    end
end

这是我的 show_responses .js.erb

$('#tweet-<%= @parent.id %>').find('.replies').append(' <%=j render "tweets/replies" %> ');

我需要将"active"参数更改为true并在我的动词文件中更新它。但是我不知道如何读取rails参数并通过ajax更改它们。

我要给链接一个ID:

<%= link_to "show replies", tweets_show_replies_path(:parent => tweet, active: false ), method: :post, remote: true, id: "tweet-#{tweet.id}-replies-link", class: "show-replies" %>

然后在show_responses .js中执行此操作。Erb更改href:

$("#tweet-#{@parent.id}-replies-link").attr("href", "<%= tweets_show_replies_path(:parent => @parent, active: true) %>");