动态地将哈希值传递给部分值

时间:2011-04-21 05:02:37

标签: ruby-on-rails ruby hash render partials

我遇到了将哈希值动态传递给部分文件的问题。我在页面上显示了人名(这是部分的)。哈希值如下:

names[1] = "James"  
names[2] = "Williams"  
names[3] = "Jack"  

render :partial => "list", :locals => {:names => names}  

我需要动态传递哈希值。我怎样才能做到这一点?在示例中,新哈希是这样的:

names[21] = 'Flink'  
names[81] = 'Mark'

我的代码


class PrlcController < ApplicationController
def bar
  @names = Hash.new
  @names[1] = "James"  
  @names[2] = "Williams"  
  @names[3] = "Jack"  
end

def foo_ajax
  @names = Hash.new
  @names[21] = 'Flink'  
  @names[81] = 'Mark'
end

end

bar.rhtml
----------

<!-- Name listing section -->
<%= render :partial => "list", :locals => "{:names => @names}" %>

<!-- AJAX calling section -->
<%= link_to_function "call testing", :onclick => "load_foo_ajax_data" %>


<!-- AJAX section -->
<div id="foo_ajax_data">
</div>

<!-- script section -->
function load_foo_ajax_data() {
       new Ajax.Updater("foo_ajax_data","/Prlc/foo_ajax", {
            asynchronous:true,
            evalScripts:true,
            onComplete:function(request){             
            },
            onLoading:function(request){}
        });
}


foo_ajax.rhtml
--------------
Some process here.


_list.rhtml
-----------
Display the names

1 个答案:

答案 0 :(得分:0)

尝试以下

class PrlcController < ApplicationController
  def bar
    @names = Hash.new
    @names[1] = "James"  
    @names[2] = "Williams"  
    @names[3] = "Jack"  
  end

  def foo_ajax
    @names = Hash.new
    @names[1] = 'Flink'  
    @names[2] = 'Mark'
    render :update do |page|
      page.replace_html  'foo_ajax_data', :partial => "list", :object => [@names]
    end
  end

end

bar.rhtml

<%= render :partial => "list", :locals => "{:names => @names}" %>    

<!-- AJAX section -->
<div id="foo_ajax_data">
  <!-- Name listing section -->
</div>

<!-- AJAX calling section -->
<%= link_to_remote "call testing", :url => { :action => "load_foo_ajax_data"} %>