Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在不重定向的情况下渲染Rails对象?_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何在不重定向的情况下渲染Rails对象?

Ruby on rails 如何在不重定向的情况下渲染Rails对象?,ruby-on-rails,Ruby On Rails,抱歉,因为我是Rails开发的新手。我想根据用户在UI中的选择来显示Rails对象。从动物和动物园的角度简化这个问题,:) 我有一个动物园的Rails模型,里面有很多动物。亲子关系 在给定的动物园“展示”页面中,我希望用户能够从下拉框中选择相关动物,并在同一页面上显示该动物的对象详细信息,即无需重定向。有没有一种简单的方法可以做到这一点?还是我必须努力让Javascript与Rails变量对话 提前感谢您的帮助,非常感谢 除了ajax之外,我想不出一个方法来完成这样的任务,无论如何,我的解决方案

抱歉,因为我是Rails开发的新手。我想根据用户在UI中的选择来显示Rails对象。从动物和动物园的角度简化这个问题,:)

我有一个动物园的Rails模型,里面有很多动物。亲子关系

在给定的动物园“展示”页面中,我希望用户能够从下拉框中选择相关动物,并在同一页面上显示该动物的对象详细信息,即无需重定向。有没有一种简单的方法可以做到这一点?还是我必须努力让Javascript与Rails变量对话


提前感谢您的帮助,非常感谢

除了ajax之外,我想不出一个方法来完成这样的任务,无论如何,我的解决方案是

由于你没有提供任何代码,我不得不做一些假设

  • 动物园和动物都只有一个属性,Name
  • 动物的信息将以无序列表的形式呈现
  • 您正在使用资产管道、coffescript和turbolinks 5
首先,您应该更新您的zoos/show.html.erb以拥有动物选择菜单和动物信息列表

app/views/zoos/show.html.erb

<p>
  <strong>Name:</strong>
  <%= @zoo.name %>
</p> 
<br>
<p>
  <strong>Animal:</strong>
  <%= select_tag "animals", options_from_collection_for_select(@zoo.animals, "id", "name"), id: 'select_animal', prompt: "Select Animal to view its info" %>
</p>

<div id="animalInfo">
  <ul>
    <li><strong>Name: </strong><span id="animalName"></span></li>
    <li><strong>Created at: </strong><span id="animalCreated"></span></li>
    <li><strong>Updated at: </strong><span id="animalUpdated"></span></li>
  </ul>
</div>
第三,将以下代码添加到Zoo脚本中,查看代码内部的解释

app/assets/javascripts/zoos.coffe

ready = ->
  $('#animalInfo').hide() # hides the animal's info div
  $('#select_animal').change -> # listen to changes in the animals select menu
    if $(this).val() 
      animalId = $(this).val()
      output = undefined
      $.ajax #calls the animal object but its id
        url: '/animals/'+ animalId + '.json',
        type: 'GET',
        dataType: 'json',
        async: false
        complete: (jqXHR, textStatus) ->
        success: (data, textStatus, jqXHR) -> #return a json animal object
          # You can call any object attribute the same way with ruby
          # Like this: data.attribute
          # Edit this part to adapt with your code
          $('#animalName').text(data.name)
          $('#animalCreated').text(data.created_at)
          $('#animalUpdated').text(data.updated_at)
          $('#animalInfo').slideDown()
        error: (jqXHR, textStatus, errorThrown) ->
    else
      $('#animalInfo').slideUp() # hides the animal's info div if no animal selected
$(document).on('turbolinks:load', ready)

我认为你应该调整上面的代码以适应你的,但概念是一样的,希望它能有所帮助。

我想不出除了ajax之外的其他方法来完成这样的任务,无论如何,我的解决方案是

由于你没有提供任何代码,我不得不做一些假设

  • 动物园和动物都只有一个属性,Name
  • 动物的信息将以无序列表的形式呈现
  • 您正在使用资产管道、coffescript和turbolinks 5
首先,您应该更新您的zoos/show.html.erb以拥有动物选择菜单和动物信息列表

app/views/zoos/show.html.erb

<p>
  <strong>Name:</strong>
  <%= @zoo.name %>
</p> 
<br>
<p>
  <strong>Animal:</strong>
  <%= select_tag "animals", options_from_collection_for_select(@zoo.animals, "id", "name"), id: 'select_animal', prompt: "Select Animal to view its info" %>
</p>

<div id="animalInfo">
  <ul>
    <li><strong>Name: </strong><span id="animalName"></span></li>
    <li><strong>Created at: </strong><span id="animalCreated"></span></li>
    <li><strong>Updated at: </strong><span id="animalUpdated"></span></li>
  </ul>
</div>
第三,将以下代码添加到Zoo脚本中,查看代码内部的解释

app/assets/javascripts/zoos.coffe

ready = ->
  $('#animalInfo').hide() # hides the animal's info div
  $('#select_animal').change -> # listen to changes in the animals select menu
    if $(this).val() 
      animalId = $(this).val()
      output = undefined
      $.ajax #calls the animal object but its id
        url: '/animals/'+ animalId + '.json',
        type: 'GET',
        dataType: 'json',
        async: false
        complete: (jqXHR, textStatus) ->
        success: (data, textStatus, jqXHR) -> #return a json animal object
          # You can call any object attribute the same way with ruby
          # Like this: data.attribute
          # Edit this part to adapt with your code
          $('#animalName').text(data.name)
          $('#animalCreated').text(data.created_at)
          $('#animalUpdated').text(data.updated_at)
          $('#animalInfo').slideDown()
        error: (jqXHR, textStatus, errorThrown) ->
    else
      $('#animalInfo').slideUp() # hides the animal's info div if no animal selected
$(document).on('turbolinks:load', ready)

我认为您应该调整上面的代码以适应您的需要,但概念是一样的,希望它能有所帮助。

这在rails应用程序中很容易做到,它使用了良好的内置AJAX处理。但是,解释相当广泛,因此我建议您阅读文档。有一个关于如何开始的教程。这在rails应用程序中使用AJAX的良好内置处理很容易做到。但是,解释相当广泛,因此我建议您阅读文档。有一个关于如何开始的教程。