使用'输入'时部分不会呈现在HTML中

时间:2014-09-01 01:47:56

标签: jquery ruby-on-rails ajax

加载DOM时我有一个Ajax调用:

/assets/javascript/items.js

if ($('#content-recommendation').length) {
    xhr = $.ajax({
    url:'/get_recommendations/' + gon.item_id + '.js',
    type:"get"
  });
}

views/items/show

<div id="content-recommendation" class="col-md-3">
  <p>hey!</p>
</div>

items_controller#get_recommendation

def get_recommendations
  respond_to do |format|
     format.js
  end
end

items/get_recommendations.js.erb

$("#content-recommendation").html("<%= render 'content_recommendation'%>")

_content_recommendation.html.erb

<h3>Last Visited Items:</h3>
<p>test</p>

到目前为止,这是我在RoR中看到的奇怪的事情。如果我输入_content_recommendation.html.erb

<h3Last Visited Items:</h3><p>test</p>

(没有空格)它有效!!

1 个答案:

答案 0 :(得分:0)

由于缺乏其他答案,或许您更愿意尝试以下方法:

#config/routes.rb
resources :items do
   get "get_recommendations/:id", action: :get_recommendations, on: :collection
end

#app/assets/javascripts/items.js
if $('#content-recommendation').length {
    $.ajax({
      url:'/get_recommendations/' + gon.item_id,
  });
}

#app/controllers/items_controller.rb
class ItemsController < ApplicationController 
    def get_recommendations
       respond_to do |format|
          format.js
       end
    end
end

#app/views/items/get_recommendations.js.erb
$("#content-recommendation").html("<%=j render partial 'content_recommendation' %>");