【问题标题】:Use ajax in rails在 Rails 中使用 ajax
【发布时间】:2016-05-30 08:40:36
【问题描述】:

这个在Rails中使用ajax的例子http://www.kunce.net/2013/10/ror-dynamic-dropdown-box-with-ajax/ 我有它不起作用,我做错了什么? 罐头必须连接 gem 还是 ustanevshy 方法不起作用? 我需要在不刷新页面的情况下获取用户在选择中选择的 divas 数据。 查看:

<div id="select_div">
    <select name="city_id" id="city_id">
      <option value="0">Lisbon</option>
      <option value="1">Madrid</option>
      <option value="2">Prague</option>
    </select>
</div>
<div id="result_div">
  <h4>Lisbon is selected</h4>
</div>

<script>
  $(document).ready(function() {
    $('#city_id').change(function() {
        $.ajax({
           url: "<%= update_text_path %>",
           data: {
               city_name: $("#city_id option:selected").text()
           },
           dataType: "script"
        });
    });
  });
</script>

routes.rb:

get "static_pages/update_text", as: "update_text"

控制器静态页面:

def update_text
    @city_text = params[:city_name]
end

views/static_pages/update_text.js.erb

$('#result_div').html("<h4>You have changed the city to: <%= @city_text %></h4>");

【问题讨论】:

  • “不起作用”是什么意思?你的浏览器有什么变化吗?您的服务器日志显示什么?
  • Lanny Bose,浏览器中的更改不在终端中写入:[2016-05-30 00:24:37] 错误错误 URI `/%3C%=%20city_id%20%%3E? city_id=2&_=1464557016658'。
  • 尝试删除“”周围的引号。可能是双引号吗?

标签: jquery ruby-on-rails ajax


【解决方案1】:

文字值&lt;%= update_text_path %&gt; 似乎被用作URL。检查您的文件是否命名为 app/views/static_pages/home.html.erb,而不是 app/views/static_pages/home.html。只有.erb 文件可以使用ERB 标记。

【讨论】:

  • 我使用 app/views/static_pages/home.html.erb
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多