2015-07-19 99 views
1

我有这个链接时触发地雷“show_replies”方法“鸣叫”控制器js.erb传递参数错误

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

,你可以看到它有两个参数活跃,它的渲染因为这

<a class="show-replies" data-remote="true" rel="nofollow" data-method="post" href="/tweets/show_replies?active=false&parent=1">show replies</a> 

show_replies方法看起来像这样

def show_replies 

    @active_param = params[:active] 
    @parent = Tweet.find(params[:parent]) 
    @tweet = Tweet.new 

    if @active_param === true 
     params[:active] = false 
    else 
     params[:active] = true 
    end 

    respond_to do |format| 
     format.html {render :nothing => true} 
     format.js 
    end 

end 

,这是我show_replies.js.erb

var parent_tweet = $('#tweet-<%= @parent.id %>'); 
$(parent_tweet).find('.replies').append(' <%=j render "tweets/replies" %> '); 
$(parent_tweet).find('.show-replies').attr('href', "<%=j tweets_show_replies_path(:parent => @parent, active: params[:active]) %>") 

但改变HREF属性与我show_replies.js.erb后链接中的参数只是在错误的格式是这样的

<a class="show-replies active" data-remote="true" rel="nofollow" data-method="post" href="/tweets/show_replies?active=true&amp;parent=1">show replies</a> 

你知道为什么js脚本添加“amp”吗?到我的属性网址?有没有办法如何解决它?因为当我点击第二个链接时,我的“show_replies”方法无法正确读取参数。

回答

0

在Rails中,不安全的值会自动转义。 &被认为是不安全的值,所以它被转移到&amp;

但我认为生成的链接不应该被转义,应该按照我的意思使用。所以,我认为你应该在这里使用的原始输出:

<%== tweets_show_replies_path(:parent => @parent, active: params[:active]) %> 

或:

<%= raw tweets_show_replies_path(:parent => @parent, active: params[:active]) %> 
+0

所以我使用不安全的或错误的做法? –

+0

在我看来错误的方法,但仅限于生成的链接。 – limekin

+0

请问您可以告诉我什么会在你的oppinion更好的方法? –