这是我的助手功能:
def edit_and_delete_links_for(model)
model_route_path = model.class.to_s.downcase
if policy(model).update?
links = link_to "Edit", self.send("edit_#{model_route_path}_path", model), class: 'btn btn-primary edit-btn'
if current_user.admin?
links += link_to "Delete", self.send("#{model_route_path}_path", model), method: :delete, class: "btn btn-danger destroy-btn"
end
end
return links
end
我试图在我的ajax结果中使用它
next.json.rb
{
"library" : <%= ActiveModel::SerializableResource.new(@library).serializable_hash.to_json.html_safe %>,
"links": {
"language" : "<%= language_path(@library.language) %>",
"framework" : "<%= framework_path(@library.framework) %>"
},
"buttons": "<%= edit_and_delete_links_for(@library).to_s %>"
}
出于某种原因,它不会逃脱课程周围的"
标记,因为你可以从我看到的铬标记中看到:
{
"library" : {"id":5,"version":null,"average_rating":"2.0","vote_count":1,"comment_count":0,"description":"Simple ORM for Sinara and Rails","reviews":[{"id":5,"rating":2,"comment":null,"user_name":"first!","updated_at":" 5:04pm on 2017-07-16 (UTC)"}]},
"links": {
"language" : "/languages/1",
"framework" : "/frameworks/2"
},
"buttons": "<a class="btn btn-primary edit-btn" href="/libraries/5/edit">Edit</a>"
}
任何想法如何逃避这一点。我试过了json_escape
但它没有做任何事情。我认为这个问题是因为它被标记为html_safe,但我还没有找到摆脱它的方法。
答案 0 :(得分:0)
在视图中调用escape_javascript
方法时,只需使用j
(或edit_and_delete_links_for
}:
"buttons": "<%= j edit_and_delete_links_for(@library).to_s %>"
或者,作为替代方案,您可以在视图中使用简单引号('
)而不是双引号("
),如下所示:
"buttons": '<%= edit_and_delete_links_for(@library).to_s %>'