如何使用 ujs、jquery 和 Rails 加载内容?
我正在尝试找出使用 Rails UJS 将内容插入灯箱的最佳方法。我有一个看起来像这样的链接:
<%= link_to "login", login_path, :remote => true %>
它会生成这样的html:
<a data-remote="true" href="/login">login</a>
所以这一切都很好,并且我创建了视图文件: user_sessions/new.js.erb
它也加载得很好,但我的问题是将 html 插入页面的首选方法是什么?就像我已经在非 js 页面上有了登录表单,那么我不能将部分加载到页面中吗?
任何想法都会受到欢迎。
I'm trying to figure out the best way to insert content into a light box using rails UJS. I have a link that looks like this:
<%= link_to "login", login_path, :remote => true %>
Which produces html as such:
<a data-remote="true" href="/login">login</a>
So this all works great, and I've created the view file: user_sessions/new.js.erb
which also loads just fine, but my question what is the preferred method of inserting appending the html into the page? Like I already have the login form on the non-js page, so can't I just load that partial into the page?
Any ideas would be very welcomed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不完全确定我明白你在问什么,但一般来说,你可以将 HTML 片段填充到一些现有容器中,如下所示:
你可以使用以下命令从服务器端操作加载 HTML 片段:
如果您有一个完整的页面,并且您只想将其部分加载到另一个页面中,您可以尝试以下操作:
这告诉 jQuery 获取加载的整个页面内容,然后查找“#something”位于整个页面内,并且仅将页面的该部分加载到目标容器中。
I'm not totally sure I understand what you're asking, but in general you can stuff fragments of HTML into some existing container like this:
You can load fragments of HTML from server-side actions with:
If you've got a complete page, and you want to load just a part of it into another page, you can try this:
That tells jQuery to take the loaded whole-page content, then look for "#something" inside that whole page, and only load that part of the page into the target container.
我最终这样做了,有点依赖于 ujs 的 jQuery 插件:
这在我的
user_sessions/new.js.erb
中,然后我制作了一个这样的 jQuery 插件:
是的,我知道那是很多容器但我必须这样做才能使模态窗口垂直居中并提供良好的双边框情况。
让我知道你对这些人的看法!
也许另一个问题是,在制作 jQuery 插件时,我该如何制作它,以便我可以仅使用
$.modal("<%= escape_javascript(render 'form') %>");< 来调用插件/code>,没有对象
this
并只调用 jQuery 对象本身的方法?I ended out doing this, sortof a jQuery plugin that relies on ujs:
This in my
user_sessions/new.js.erb
then I made a jQuery plugin as such:
And yah I know thats a lot of containers but I had to do this to center the modal window vertically and provide a nice double border situation.
Let me know what you think about this folks!
Perhaps an additional question would be when making a jQuery plugin how can I make it so I can call the plugin with just
$.modal("<%= escape_javascript(render 'form') %>");
, without the objectthis
and just call the method on the jQuery object itself?