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

发布于 2024-11-02 18:45:24 字数 1349 浏览 4 评论 0原文

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

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

I faced a problem to pass the hash value to partial file dynamically. I displayed the person names on my page (this is a partial). The hash value is like this:

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

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

I need to pass the hash value dynamically. How can I do that? In the example, the new hash is like this:

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

My code


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情哥ㄟ 2024-11-09 18:45:24

尝试以下

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

Try following

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"} %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文