【问题标题】:Rails pass ID from auto_complete field to observe_fieldRails 将 ID 从 auto_complete 字段传递到 observe_field
【发布时间】:2010-12-22 01:07:22
【问题描述】:
<p>Song Name:<%= text_field :songlists, :song_name, :id => "songlists_" + section.id.to_s + "_song_name" %></p>

<%= auto_complete_field "songlists_" + section.id.to_s + "_song_name", :method => :get,
        :with => "'search=' + element.value", :url => formatted_songlists_path(:js)  %>

<p>Artist:<%= text_field :songlists, :artist, :id => "songlists_" + section.id.to_s + "_artist" %></p>

<%= observe_field  ("songlists_" + section.id.to_s + "_song_name", :update => "songlists_" + section.id.to_s + "_artist", :with => "element.value" , :url => { :controller => :songlists, :action => "get_artist"  } ) %>

我的自动完成字段工作正常,观察字段根据萤火虫触发,但我不确定如何从观察字段更新文本字段。

我需要获取自动完成功能找到的 ID,或者甚至是输入的文本,以便我可以将其传递到观察字段的 :url。

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails autocomplete


    【解决方案1】:

    你能解释一下你想让这段代码做什么吗?

    【讨论】:

      【解决方案2】:

      我正在尝试从自动完成字段中获取响应,以使用结果更新另一个文本字段。我现在在 firebug 中有响应,但我似乎无法让它用结果更新框。

      <p>Song Name:<%= text_field :songlists, :song_name, :id =>
      "songlists_" + section.id.to_s + "_song_name" %></p>
      
      
      <%= auto_complete_field "songlists_" + section.id.to_s +
      "_song_name",      :method => :get,
                         :with => "'search=' + element.value", 
                         :url => formatted_songlists_path(:js)  %>
      
      <p>Artist:<%= text_field :songlists, :artist %></p>
      
      <%= observe_field  "songlists_" +section.id.to_s + "_song_name",
                         :frequency => 0.5, 
                         :url => { :controller => :songlists, 
                         :action => :get_artist }, 
                         :update => "songlists_artist", 
                         :with => "song_name" %>
      

      控制器代码

      def get_artist
      @songlist = Songlist.find(:all, :conditions => ['name = ?', params[:song_name]])
      
      if !@songlist.empty?
      
        render :update do |page|
          page.replace_html 'songlists_artist', :partial =>
          'get_artist', :object => @songlist
       end
      
      end
      
      end
      

      在萤火虫中我看到了这个响应

      try {
      Element.update("songlists_artist", "Robbie Williams");
      } catch (e) { alert('RJS error:\n\n' + e.toString());
      alert('Element.update(\"songlists_artist\", \"Robbie Williams\");');
      throw e }
      

      但它没有更新我的文本字段,有什么想法吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 2015-07-02
        相关资源
        最近更新 更多