简体   繁体   English

Rails控制器动作未调用

[英]rails controller action not being called

The problem I'm running into deals with rendering a partial. 我遇到的问题是渲染局部图像。 I basically have a link which should get updated after the user puts some text in a text field. 我基本上有一个链接,该链接应在用户在文本字段中输入一些文本后进行更新。 The first time it works fine. 第一次工作正常。 The user types in the text, the link gets updated and then when the link is clicked the partial is rendered with the correct information. 用户输入文本,链接将更新,然后单击链接时,将使用正确的信息呈现部分链接。 However when the text is changed the link also gets update with the new parameters to pass to the action but when the link is clicked it still renders the old partial and no call to the corresponding action is made. 但是,当更改文本时,链接也将使用新参数进行更新以传递给操作,但是当单击链接时,它仍会呈现旧的部分内容,并且不会调用相应的操作。 Here is my view code: 这是我的查看代码:

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

And this is my partial which I am rendering: 这是我正在渲染的部分内容:

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

I'm displaying the partial with the information in a lightbox type display. 我正在灯箱类型的显示器中显示部分信息。 Here is my javascript code for the onBlur event of the text field: 这是文本字段的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)
          }
        });
      }
    }

So in the javascript code when I look at the output of the href attribute in the console, it shows the correct link. 因此,在javascript代码中,当我在控制台中查看href属性的输出时,它显示了正确的链接。 And finally here is my controller code: 最后是我的控制器代码:

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

The color_list variable is an array of colors which I send back in order to update the link with. color_list变量是我发送回以更新链接的颜色数组。 I know that the show_colors action gets called called the first time because the @colors variable's value is printed in my terminal however when I update the text in the textfield and I click on the link again the action is not being called even though the link gets updated because nothing is printed in the terminal. 我知道第一次调用show_colors操作是因为@colors变量的值已在我的终端中打印,但是当我更新文本字段中的文本并再次单击该链接时,即使该链接被获取,也不会调用该操作已更新,因为终端上没有任何内容。 It seems as if the partial is being cached, if that is the problem how can I prevent that. 看起来好像部分缓存了,如果那是问题,我该如何防止。 Also when I try to render my partial as a full fledged view rather than a partial, the action is called correctly every time and it renders the correct information even after changing the text field contents, but when I do that then my lightbox functionality does not work correctly. 同样,当我尝试将部分视图呈现为完整视图而不是部分视图时,每次都会正确调用该操作,即使更改了文本字段内容,该操作也会呈现正确的信息,但是当我这样做时,我的灯箱功能不会正常工作。 Any help will be appreciated, thank you. 任何帮助将不胜感激,谢谢。

hi guys for anyone who is interested I figured it out. 嗨,有兴趣的人,我想通了。 It is kind of hacky but it works for me. 这有点hacky,但对我有用。 I added this piece of code in my javascript outside of all functions, and it cleared the cached ajax. 我在所有功能之外的JavaScript中添加了这段代码,并清除了缓存的Ajax。

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

Hopefully it will save someone time in the future. 希望它将在将来节省某人的时间。

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

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