I can't believe I've been looking four hours for this simple task, but I have.
我不敢相信我这个简单的任务已经花了四个小时,但我有。
In Rails 2.3, I could replace one section of a page with this simple code:
在Rails 2.3中,我可以用这个简单的代码替换页面的一个部分:
render :update do |page| page.replace_html "div_id", :partial => "new_content",... end
渲染:更新do | page | page.replace_html“div_id”,:partial =>“new_content”,...结束
In Rails 3, Ryan Bates has me writing entire new javascript functions, switching from Prototype (rails default) to jQuery, and otherwise not enjoying life. The other tutes are no more straightforward.
在Rails 3中,Ryan Bates让我编写了全新的javascript函数,从Prototype(rails default)切换到jQuery,否则就无法享受生活。其他的tutes不再那么简单。
What am I missing? How do we replace a <div>
these days?
我错过了什么?这几天我们如何替换
6 个解决方案
#1
12
Thanks, guys. The official answer seems to be that, yes, the team felt simple is the enemy of good and made it more complicated.
多谢你们。官方的答案似乎是,是的,团队觉得简单是好的敌人,使其变得更加复杂。
The first key is to create a .js.erb file NAMED for the method CALLING the ajax update. So if the index method handles the update, put the raw javascript in index.js.erb
. This goes in the views folder.
第一个关键是为方法调用ajax更新创建一个.js.erb文件NAMED。因此,如果索引方法处理更新,请将原始javascript放在index.js.erb中。这将在views文件夹中。
Second, the code that worked in index.js.erb was
其次,在index.js.erb中工作的代码是
m = $('list_users');
m.innerHTML = "<%= escape_javascript(render :partial => "reload_users") %>";
Then to make the call, add in the respond_to block of the controller method, add:
然后进行调用,添加控制器方法的respond_to块,添加:
format.js
Finally, the calling view has:
最后,调用视图具有:
<%= link_to "Update User List", @reload_users_path, :remote => true %>
By the way, supposedly all the old pages using page.replace
will work if you install a plugin. The plugin download page suggests that it broke in the last releases of Rails 3 and has not been fixed. Also, various bloggers will come to your home and birch-switch you if you use it.
顺便说一下,如果安装插件,所有使用page.replace的旧页面都可以使用。插件下载页面表明它在Rails 3的最新版本中已经破解并且尚未修复。此外,如果您使用它,各种博客将来到您的家,并桦树切换您。
#2
4
The whole RJS stuff makes the javascript inline and makes the dom very obtrusive. Also, by avoiding inline javascript you could open up other possible ways of optimizing you javascript by compressing and caching those files in browser. Thats the reason why RJS is getting out of scope from rails 3. A little bit of getting around with jQuery or Prototype for a day should get you on gears with these kind of small stuff and will help the project on long run.
整个RJS的东西使javascript内联并使dom非常突兀。此外,通过避免内联JavaScript,您可以通过在浏览器中压缩和缓存这些文件来打开其他可能的优化JavaScript的方法。这就是为什么RJS超出轨道3的原因。一天使用jQuery或Prototype的一小部分应该让你开始使用这些小东西,并将长期帮助项目。
#3
1
Do you still have jQuery in there? I'd recommend it over Prototype any day...
你还有jQuery吗?我会在任何一天推荐Prototype
If it's still there you can just use the following in your Javascript:
如果它仍然存在,您可以在Javascript中使用以下内容:
$.get("<%= url_for path/to/partial %>",
function(response) {
$("#div_id").html(response);
});
This gets the partial via AJAX and just dumps it into the div with id div_id.
这通过AJAX获取部分并将其转储到id为div_id的div中。
Hope this helps!
希望这可以帮助!
#4
0
I'm not even sure you need to make an AJAX call to load that partial. I believe that in a js.erb file, a call to render(:partial => object_or_path) will just return a string with all the html, which you can wrap in a jQuery object and append. Example:
我甚至不确定你是否需要进行AJAX调用来加载那部分内容。我相信在js.erb文件中,对render(:partial => object_or_path)的调用将只返回一个包含所有html的字符串,您可以将其包装在jQuery对象中并追加。例:
$('#div_id').html($('<%= render :partial => @object %>'))
#5
0
As far as I know, along the same line as the answer above, you can do something like this in your template:
据我所知,与上面的答案一样,您可以在模板中执行以下操作:
<%= link_to "Update User List", @reload_users_path, :remote => true %>
And in controller, do this:
在控制器中,执行以下操作:
respond_to do |format|
format.js {
render :text => "alert('reloaded')"
}
end
This way you can have controller "execute" client side JS much the same as as render :update used to do. This is equivalent to doing the following in Rails 2:
这样你就可以让控制器“执行”客户端JS和render:update一样。这相当于在Rails 2中执行以下操作:
render :update do |page|
page << "alert('reloaded')"
end
Is there any reason why this approach is not advisable?
有什么理由不采用这种方法吗?
#6
-2
Try this:
尝试这个:
page.call "$('#div_id').html", render(:partial => 'new_content')
#1
12
Thanks, guys. The official answer seems to be that, yes, the team felt simple is the enemy of good and made it more complicated.
多谢你们。官方的答案似乎是,是的,团队觉得简单是好的敌人,使其变得更加复杂。
The first key is to create a .js.erb file NAMED for the method CALLING the ajax update. So if the index method handles the update, put the raw javascript in index.js.erb
. This goes in the views folder.
第一个关键是为方法调用ajax更新创建一个.js.erb文件NAMED。因此,如果索引方法处理更新,请将原始javascript放在index.js.erb中。这将在views文件夹中。
Second, the code that worked in index.js.erb was
其次,在index.js.erb中工作的代码是
m = $('list_users');
m.innerHTML = "<%= escape_javascript(render :partial => "reload_users") %>";
Then to make the call, add in the respond_to block of the controller method, add:
然后进行调用,添加控制器方法的respond_to块,添加:
format.js
Finally, the calling view has:
最后,调用视图具有:
<%= link_to "Update User List", @reload_users_path, :remote => true %>
By the way, supposedly all the old pages using page.replace
will work if you install a plugin. The plugin download page suggests that it broke in the last releases of Rails 3 and has not been fixed. Also, various bloggers will come to your home and birch-switch you if you use it.
顺便说一下,如果安装插件,所有使用page.replace的旧页面都可以使用。插件下载页面表明它在Rails 3的最新版本中已经破解并且尚未修复。此外,如果您使用它,各种博客将来到您的家,并桦树切换您。
#2
4
The whole RJS stuff makes the javascript inline and makes the dom very obtrusive. Also, by avoiding inline javascript you could open up other possible ways of optimizing you javascript by compressing and caching those files in browser. Thats the reason why RJS is getting out of scope from rails 3. A little bit of getting around with jQuery or Prototype for a day should get you on gears with these kind of small stuff and will help the project on long run.
整个RJS的东西使javascript内联并使dom非常突兀。此外,通过避免内联JavaScript,您可以通过在浏览器中压缩和缓存这些文件来打开其他可能的优化JavaScript的方法。这就是为什么RJS超出轨道3的原因。一天使用jQuery或Prototype的一小部分应该让你开始使用这些小东西,并将长期帮助项目。
#3
1
Do you still have jQuery in there? I'd recommend it over Prototype any day...
你还有jQuery吗?我会在任何一天推荐Prototype
If it's still there you can just use the following in your Javascript:
如果它仍然存在,您可以在Javascript中使用以下内容:
$.get("<%= url_for path/to/partial %>",
function(response) {
$("#div_id").html(response);
});
This gets the partial via AJAX and just dumps it into the div with id div_id.
这通过AJAX获取部分并将其转储到id为div_id的div中。
Hope this helps!
希望这可以帮助!
#4
0
I'm not even sure you need to make an AJAX call to load that partial. I believe that in a js.erb file, a call to render(:partial => object_or_path) will just return a string with all the html, which you can wrap in a jQuery object and append. Example:
我甚至不确定你是否需要进行AJAX调用来加载那部分内容。我相信在js.erb文件中,对render(:partial => object_or_path)的调用将只返回一个包含所有html的字符串,您可以将其包装在jQuery对象中并追加。例:
$('#div_id').html($('<%= render :partial => @object %>'))
#5
0
As far as I know, along the same line as the answer above, you can do something like this in your template:
据我所知,与上面的答案一样,您可以在模板中执行以下操作:
<%= link_to "Update User List", @reload_users_path, :remote => true %>
And in controller, do this:
在控制器中,执行以下操作:
respond_to do |format|
format.js {
render :text => "alert('reloaded')"
}
end
This way you can have controller "execute" client side JS much the same as as render :update used to do. This is equivalent to doing the following in Rails 2:
这样你就可以让控制器“执行”客户端JS和render:update一样。这相当于在Rails 2中执行以下操作:
render :update do |page|
page << "alert('reloaded')"
end
Is there any reason why this approach is not advisable?
有什么理由不采用这种方法吗?
#6
-2
Try this:
尝试这个:
page.call "$('#div_id').html", render(:partial => 'new_content')