Rails:link_to_remote 更新不会渲染视图
我有一个 link_to_remote 来呈现对象的编辑操作。 但它所做的只是使用此响应更新 Dom 元素
try { } catch (e) { alert('RJS error:\n\n' + e.toString()); alert(''); throw e }
我的链接如下所示:
= link_to_remote t("txt.edit"), :update => dom_id(comment), :url => edit_comment_path(comment.id)
我在评论控制器中的编辑操作:
# GET /comments/1/edit
def edit
@comment = Comment.find(params[:id])
respond_to do |format|
format.html
format.js { render :action => "edit" }
end
end
根据日志,请求似乎没问题:
Processing CommentsController#edit (for 127.0.0.1 at 2009-04-08 18:55:36) [GET]
Session ID: 1d4b9b3d3319d5cd556d00d2e053b651
Parameters: {"authenticity_token"=>"5d70f9e5beded361ee7e87ee591512411e8f3eec", "id"=>"18"}
User Columns (2.0ms) SHOW FIELDS FROM `users`
User Load (0.5ms) SELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
Account Columns (1.6ms) SHOW FIELDS FROM `accounts`
Account Load (0.2ms) SELECT * FROM `accounts` WHERE (`accounts`.`subdomain` = 'xxx') LIMIT 1
Comment Columns (1.7ms) SHOW FIELDS FROM `comments`
Comment Load (0.6ms) SELECT * FROM `comments` WHERE (`comments`.`id` = 18)
Rendering comments/edit
Completed in 30818ms (View: 2, DB: 7) | 200 OK [http://xx.xxx.rails/comments/18/edit?authenticity_token=5d70f9e5beded361ee7e87ee591512411e8f3eec]
我做错了什么? 谢谢您的帮助!
更新: 顺便说一句,它可以与 RJS 模板配合使用 - 这就是我现在解决它的方法。 但我仍然更喜欢渲染视图的解决方案。 否则我必须为此目的创建一个部分(我无法使用 rjs 页面更新渲染视图 - 至少我不知道如何)。
I have a link_to_remote to render the edit action of an object. But all it does is update the Dom Element with this response
try { } catch (e) { alert('RJS error:\n\n' + e.toString()); alert(''); throw e }
My link looks like this:
= link_to_remote t("txt.edit"), :update => dom_id(comment), :url => edit_comment_path(comment.id)
My edit action in the comment controller:
# GET /comments/1/edit
def edit
@comment = Comment.find(params[:id])
respond_to do |format|
format.html
format.js { render :action => "edit" }
end
end
The request seems to be ok, according to the log:
Processing CommentsController#edit (for 127.0.0.1 at 2009-04-08 18:55:36) [GET]
Session ID: 1d4b9b3d3319d5cd556d00d2e053b651
Parameters: {"authenticity_token"=>"5d70f9e5beded361ee7e87ee591512411e8f3eec", "id"=>"18"}
User Columns (2.0ms) SHOW FIELDS FROM `users`
User Load (0.5ms) SELECT * FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
Account Columns (1.6ms) SHOW FIELDS FROM `accounts`
Account Load (0.2ms) SELECT * FROM `accounts` WHERE (`accounts`.`subdomain` = 'xxx') LIMIT 1
Comment Columns (1.7ms) SHOW FIELDS FROM `comments`
Comment Load (0.6ms) SELECT * FROM `comments` WHERE (`comments`.`id` = 18)
Rendering comments/edit
Completed in 30818ms (View: 2, DB: 7) | 200 OK [http://xx.xxx.rails/comments/18/edit?authenticity_token=5d70f9e5beded361ee7e87ee591512411e8f3eec]
What am I doing wrong? Thanks for the help!
UPDATE:
It works by the way with a RJS template - that's how I solved it now. But I still prefer a solution where a view is rendered. Otherwise I have to create a partial just for this purpose (I cannot render Views with rjs page updates - at least I don't know how).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
或者根本不捕捉“respond_to”,因为在这两种情况下你都在做同样的事情! 直接注释掉:
Or do not catch "respond_to" at all since in both cases You are doing the same! Just comment out:
将其添加到
link_to_remote
调用中,它应该使 Rails 评估返回的任何脚本,而不是输出它们纯文本。Add this to the
link_to_remote
call and it should make rails evaluate any scripts returned, rather than outputting them plain-text.您忽略提及的一件事是模板是什么......我猜这就是问题所在。 如果是 ERB,请尝试以下操作:
The one thing you neglected to mention is what the template is... I'm guessing that's the problem. If it's ERB try something like:
看来您可能忘记了在视图中包含对 Javascript 库的引用,并且视图正在渲染文字 JavaScript,而不是解释它。
It looks like you may have forgotten to include a reference to the Javascript libraries in your views, and the view is rendering the literal JavaScript instead of interpreting it.
当你响应 js 请求时,
render :action =>; "edit"
将尝试渲染 edit.rjs(一个 rjs '模板'),而不是 edit.haml(一个 html 模板)。When you are responding to a js request,
render :action => "edit"
will try to render edit.rjs, a rjs 'template', not edit.haml, an html template.从 RJS 标记中删除 :update 选项。 我在那里有一个,当我尝试 insert_html 时,它显示从方法返回的 JS,而不是在 DOM 上执行它。 所以它显示 => try {...}
在我看来,我从 link_to_remote 语句中取出了 :update 属性,使用了
渲染:更新|page|
块,它执行返回的 JS,而不是显示它。page.insert_html(...)
end
希望有帮助。
Remove the :update option from your RJS tag. I had one in there and when I was trying to insert_html it was displaying the JS returned from the method and not executing it on the DOM. So it was displaying => try {...}
I took the :update attribute out of my link_to_remote statement in my view, used a
render: update do |page|
block and it executed the JS returned instead of displaying it.page.insert_html(...)
end
Hope that helps.