通过在轨道上的JavaScript文件红宝石渲染局部视图红宝石、视图、局部、轨道

2023-09-10 21:52:15 作者:在你眼里我是什么。

我有一个包含谷歌地图只(图通过JavaScript)的索引文件。当我搜索一些地方,还销其附近的地图,现在是很好的。但我要的是,当过我点击标记在地图上。它应该显示的地方细节的DIV地图下方在同一页上。我在阿贾克斯的资产/ JavaScript的/ customer.js,指向URL在控制器/客户/索引的客户控制器,我得到了整个页面的响应。这就是我们所知道的是错的。这是我的js文件内容

I have an index file which contains google map just (map through javascript) . when I search some place, it also pins its nearby on the map that's good now . But what I want is , when ever I click on marker on the map . It should show a div of place detail on the same page below the map . I made ajax assets/javascript/customer.js , pointing the URL at customer controller in controller/customers/index I get the whole page in response . that's we know is wrong . Here is my js file content

$.ajax({
  url: "/",
  async: false,
  method: "post",
  data: { "place_id" : place["place_id"] },
  success: function(data){
       alert('success');
     }
   });
   infoWindow.open(map, marker);
   buildIWContent(place);
});

和我的控制器的功能是

def index
  if params[:place_id]
    @place = Places.find(params['place_id'])
  end
end

这就是我要在我的索引页即html.erb

And this is what I want to show in my index page i.e html.erb

<% if @place %>
  <div class='place-name'><% @place.name %></div>
<% end %>

我知道有在轨支持的谐音,但我不知道那些将工作或没有。我是新来的Rails所以原谅我,如果你发现我的问题很愚蠢的。 我只是想表明通过AJAX魔术这个地名分区,无论是通过谐音,或只是因为它是。

I know there are partials supported in rails but I don't know whether those will work or not . I'm new to Rails so pardon me if you find my question very silly. I just want to show this place-name div through ajax magic, whether through partials or just as it is.

推荐答案

要解决你的问题,你不得不修改了一些code在这里。首先,你必须修订您的Ajax请求方法和网址为:

To fix you problem, you have to revised some code here. First, you have to revised your ajax request method and url into:

$.ajax({
  url: "/customers",
  async: false,
  method: "GET",
  data: { "place_id" : place["place_id"] },
  success: function(data){
       alert('success');
     }
   });
   infoWindow.open(map, marker);
   buildIWContent(place);
});

它会显示警告,当你做访问用户索引页面。 然后。在您的索引方式,尽量要做到这一点:

It will show alert when you've done to access the customers index page. Then. In your index method, try to do this:

def index
  if params[:place_id]
    @place = Places.find(params['place_id'])
  end
  respond_to do |format|
    format.html
    format.js
  end
end

然后。你必须创建新的文件 index.js.erb的包含

$('.place-name').text('<%= @place %>');

这只是雏形。你必须用自己的方式来定制

It's only prototype. You have to customize with your own way