在 Rails 中将部分集合渲染为 html
我正在使用 jQuery 对我的控制器进行 ajax 调用,一切都运行良好,只是我无法完全获得我想要的返回值。
假设我有一个项目集合,并且有部分项目:
/app/views/messages/_message.html.erb
我正在尝试将其渲染为一大串文本/html,如下所示:
format.js { render_to_string :partial => "message", :collection => @messages}
但是,这是渲染某种视图,因为我正在获取和标签。即使我将其更改为:
format.js { render_to_string :partial => "message", :collection => @messages, :layout => 'none'}
它仍然返回相同的内容。这可能吗?我需要返回它以便 jQuery 来处理它,所以我不确定这是否可以用 rjs 来完成。任何帮助表示赞赏。
I am using jQuery to do an ajax call to my controller, and everything is working perfectly, except I can't quite get the return value that I want.
Let's say I have a collection of items, and I have the partial:
/app/views/messages/_message.html.erb
I am trying to render it as a big string of text/html like so:
format.js { render_to_string :partial => "message", :collection => @messages}
However, this is rendering a view of some sort, because I'm getting and tags. Even if I change it to:
format.js { render_to_string :partial => "message", :collection => @messages, :layout => 'none'}
it still returns the same thing. Is this possible? I need to return it for jQuery to handle it, so I'm not sure if this can be done with rjs. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过仅使用 render 而不是 render_to_string ?
另外,您使用 jQuery 的事实使这稍微复杂一些,如果您使用原型,您可以使用 rjs 并执行类似的操作
Have you tried just using render instead of render_to_string?
Also, the fact that you're using jQuery makes this slightly more complicated, if you were using prototype you could use rjs and do something like