我得到了这个链接
<%= 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/