rails控制器动作未被调用

时间:2014-03-10 16:57:55

标签: ruby-on-rails caching controller renderpartial

我遇到了渲染部分问题的问题。我基本上有一个链接,在用户将一些文本放入文本字​​段后应该更新。它第一次工作正常。用户键入文本,链接得到更新,然后单击链接时,将使用正确的信息呈现部分。但是,当文本被更改时,链接也会使用新参数进行更新以传递给操作,但是当单击链接时,它仍会呈现旧的部分,并且不会调用相应的操作。这是我的观看代码:

label.control-label for="color_servers" Colors
        .controls
          input.input-xxlarge type="text" name="colors" id="project_colors" placeholder="comma separated colors" onblur="getColors(this)"
          a data-toggle="modal" id="color_target" href="#" data-target="#color_modal" (Show colors)

.modal.hide#color_modal
  .modal-header
    button.close type="button" data-dismiss="modal" aria-hidden="true" ×
    h3 Color list
  .modal-body
  .modal-footer
    a.btn href="#" data-dismiss="modal" aria-hidden="true" Close

这是我正在渲染的部分内容:

ul
    - for color in @colors
        li #{color}

我在灯箱类型显示中显示包含信息的部分。以下是我的文本字段onBlur事件的javascript代码:

function getColors(elem){
      if(elem.value.trim()){
        $.ajax({
          url: "/project/check_colors",
          type: "GET",
          dataType: "json",
          data:{
            colors: elem.value
          },
          success: function(data){
            $('#color_target').attr('href','/project/show_colors?colors=' + data.color_list)
            console.log(document.getElementById("color_target").href)
            console.log(data.input)
            console.log(data.color_list)
          }
        });
      }
    }

因此,在javascript代码中,当我查看控制台中href属性的输出时,它会显示正确的链接。最后这是我的控制器代码:

def check_colors
    colors = params[:colors].gsub(/\s+/, "").gsub(/,+/,",")
    colors = colors.chomp(",")
    color_list = Color.expand_colorset(colors).map(&:fullname)
    render 'index', :json => { 
                                :servers => color_list,
                                :input => colors    
                             }
  end

  def show_colors
    colors_string = params[:colors]
    @colors = colors_string.split(",")
    puts @colors
    render partial: 'color_list'
  end

color_list变量是我发回的颜色数组,以便更新链接。我知道第一次调用show_colors操作,因为我的终端中打印了@colors变量的值,但是当我更新文本字段中的文本并再次点击链接时即使链接更新因为终端中没有打印任何内容,也不会调用该操作。似乎部分被缓存,如果这是问题,我该如何防止这种情况。此外,当我尝试将部分渲染为完全成熟的视图而不是部分视图时,每次都会正确调用操作,即使在更改文本字段内容后它也会呈现正确的信息,但是当我这样做时,我的灯箱功能不会工作正常。任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

嗨,对于任何有兴趣的人,我都明白了。它有点像hacky但它​​对我有用。我在所有函数之外的javascript中添加了这段代码,并清除了缓存的ajax。

$('body').on('hidden', '.modal', function(){$(this).removeData('modal');});

希望将来能节省一些时间。

相关问题