Rails将ID从auto_complete字段传递给observe_field

时间:2009-12-01 14:37:40

标签: ruby-on-rails autocomplete

<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"  } ) %>

我的自动完成字段工作正常,并且观察字段根据firebug触发,但我不确定如何更新观察字段中的文本字段。

我需要获取自动完成找到的ID,甚至是输入的文本,以便我可以将其传递到observe-field的:url。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

你能说一下你想要这个代码做什么吗?

答案 1 :(得分:0)

我正在尝试从自动完成字段获取响应,以使用结果更新另一个文本字段。我现在在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 }

但它没有更新我的文本字段,任何想法?