简体   繁体   English

通过Rails上的javascript文件ruby渲染部分视图

[英]Rendering partial view through javascript file ruby on rails

I have an index file which contains google map just (map through javascript) . 我有一个索引文件,其中仅包含google map(通过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 . 我制作了ajax资产/javascript/customer.js,将URL指向controller / customers / index中的客户控制器,然后得到整个页面作为响应。 that's we know is wrong . 那就是我们知道是错误的。 Here is my js file content 这是我的js文件内容

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

And my controller function is 我的控制器功能是

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

And this is what I want to show in my index page ie html.erb 这就是我想在索引页面中显示的内容,即html.erb

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

I know there are partials supported in rails but I don't know whether those will work or not . 我知道Rails支持某些局部,但是我不知道这些局部是否起作用。 I'm new to Rails so pardon me if you find my question very silly. 我是Rails的新手,所以如果您发现我的问题很傻,请原谅我。 I just want to show this place-name div through ajax magic, whether through partials or just as it is. 我只是想通过ajax魔术来显示此地名div,无论是通过局部还是通过它。

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修改为:

$.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

Then. 然后。 You have to create new file index.js.erb contains 您必须创建新文件index.js.erb包含

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

It's only prototype. 这只是原型。 You have to customize with your own way 您必须以自己的方式进行自定义

If I understand your question correctly you want to render a Rails partial as the response to an AJAX call. 如果我正确理解了您的问题,则希望将Rails部分渲染为对AJAX调用的响应。 If that's the case, then that's the wrong way to think about it. 如果是这样的话,那就是错误的思考方式。

Rails is just a web server. Rails只是一个Web服务器。 It returns text payloads that can be JSON, XML, or HTML (among other things). 它返回的文本有效载荷可以是JSON,XML或HTML(以及其他)。 A partial or ERB template is just an abstraction for that generated JSON or HTML. 部分模板或ERB模板只是生成的JSON或HTML的抽象。

When you load the page, Rails compiles the ERB (and partials, layouts, etc) into an HTML payload and returns that. 当您加载页面时,Rails将ERB(以及部分,布局等)编译为HTML有效负载并将其返回。 So if you were to make an AJAX request to an endpoint, you'd get all of the HTML (including layouts). 因此,如果要向端点发出AJAX请求,则将获得所有HTML(包括布局)。 You could do something like render layout: false to not render the layout, but then you need a conditional or a separate endpoint to know when to render the layout and when not to. 您可以执行类似render layout: false表示不渲染布局,但是您需要一个条件端点或单独的端点才能知道何时渲染布局以及何时不渲染。

In general the community seems to be moving away from this for AJAX requests preferring client-side templating from JSON server responses, but you can still do it by adding layout: false (or whatever layout you want) 通常,对于AJAX请求,社区似乎正在朝此方向发展,而倾向于从JSON服务器响应中进行客户端模板化,但是您仍然可以通过添加layout: false (或所需的任何布局)来做到这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM