简体   繁体   English

url_for和远程:true

[英]url_for and remote: true

I've got an issue with Rails. 我遇到了Rails的问题。 I want a select form which render a partial view when clicked. 我想要一个选择表单,该表单在单击时呈现局部视图。 I want the partial views to be effective without refreshing the whole document (Ajax) but it seems that remote: true does not work for me... 我希望部分视图在不刷新整个文档(Ajax)的情况下有效,但是似乎remote: true对我不起作用...

What I have currently and refresh the whole document: 我目前拥有的内容和刷新的整个文档:

= form_tag url_for(action: :index), method: 'get' do
    //Storing some informations in html fields for further use
    - shops_array = @shops.collect.with_index { |shop, i| [shop.title, shop.id, {'address' => [shop.address, shop.zipcode, shop.city].join(" ")}, {'id' => 'shop'+String(i)}] }
    = select_tag :shop_id, options_for_select(shops_array, @shop_id), prompt: "Your shop :", data: {submit_on_change: true}

What I've tried : 我尝试过的

= form_tag url_for(action: :index), remote: true, method: 'get' do
    //Storing some informations in html fields for further use
    - shops_array = @shops.collect.with_index { |shop, i| [shop.title, shop.id, {'address' => [shop.address, shop.zipcode, shop.city].join(" ")}, {'id' => 'shop'+String(i)}] }
    = select_tag :shop_id, options_for_select(shops_array, @shop_id), prompt: "Your shop :", data: {submit_on_change: true}

How do I apply the remote: true option with url_for ? 如何使用url_for应用remote: true选项? If not possible, I could use some help finding a workaround. 如果不可能,我可以使用一些帮助找到解决方法。

As per the docs you can specify the remote option to the form_tag method, like so: 根据文档,您可以为form_tag方法指定remote选项,如下所示:

form_tag(url_for(controller: :charmander, action: :ember), remote: true, method: :get, class: 'pokemon-form') do
  # form logic
end

However , this will only trigger the ajax:success event when the whole form is submitted. 但是 ,仅在提交整个表单时才会触发ajax:success事件。

From your question it seems to me that you want to re-render some partial when your select value changes. 从您的问题看来,在您选择的值更改时,您似乎想重新渲染部分内容。 I'm not sure how you use the submit_on_change data attribute, but if I'm correct in assuming that you make it submit the entire form when the select vaule changes then all you need to do is listen for the ajax:success event for the entire form, like so: 我不确定您如何使用submit_on_change数据属性,但是如果我正确地假设当选择值更改时您让它提交整个表单,那么您需要做的只是监听ajax:success事件。完整的形式,像这样:

$('.pokemon-form').on('ajax:success', function (event, data, status, xhr) {
  // Do something with data, which would be the rendered partial returned
  // from the server call.
});

Ended up using an onclick and a JavaScript function. 最终使用了onclick和JavaScript函数。 Though it may not be the best solution nor the prettier, it works like a charm for me. 尽管它可能不是最佳解决方案,也不是更漂亮,但对我来说,它就像是一种魅力。

= select_tag :shop_id, options_for_select(shops_array, @shop_id), prompt: "Your shop :", onclick: 'ajaxSelectedShop('+String(8)+')'

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

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