动态地将哈希值传递给部分
我遇到了将哈希值动态传递给部分文件的问题。我在我的页面上显示了人名(这是部分)。哈希值是这样的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下
bar.rhtml
Try following
bar.rhtml