javascript - 通过 AJAX 更改 link_to 参数

标签 javascript jquery ruby-on-rails ruby ajax

我得到了这个链接

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

这个功能在我的“tweets” Controller 中

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_replies.js.erb

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

我需要将“active”参数更改为 true 并在我的 erb 文件中更新它。但我不知道如何读取 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_replies.js.erb 中执行此操作以更改 href:

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

关于javascript - 通过 AJAX 更改 link_to 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31431835/

相关文章:

javascript - 当按下按钮进入输入时获取表格 div 内容

javascript - Shiny /DT : Show Child Rows Upon Initial Load

javascript - 通过jquery访问动态元素

javascript - 使用 Backbone 上传文件

javascript - jQuery $(document).ready 在 Iron/Chrome 中损坏

jQuery - 从表的最后一行获取输入字段值?

ruby-on-rails - 从 Rail Active 调用 Socket.io :Record callback or observer

ruby-on-rails - RESTful 网站与 RESTful API - 有什么区别,重要吗?

ruby-on-rails - Rails Geocoder Gem 如何访问 Google API?

javascript - 有没有办法覆盖 vue 组件内的转换