改进“remote_function”使用“高级”阿贾克斯
我正在使用 Ruby on Rails 3,并尝试使用 remote_function
方法改进视图文件中的 AJAX 请求。当鼠标指针位于
上时,以下代码将显示文本 Fire AJAX
并且将单击 Fire AJAX
时调用操作控制器方法。
在视图文件中,我有:
<div id="test_id_div">
<span id="test_id_span" %> style="display: none">
<%= link_to_function 'Fire AJAX', remote_function(:url => {:action => :action_method_name, :controller => 'controller_name'}) %>
</span>
<%=
update_page_tag do |page|
page.event.observe("test_id_div", 'mouseover') do |element|
element[("test_id_span").to_sym].show
end
page.event.observe("test_id_div", 'mouseout') do |element|
element[("test_id_span").to_sym].hide
end
end
%>
</div>
上面的代码可以工作,但是我想以某种“更好”的方式改进它(例如:编写更少的代码,...)。 我怎样才能做到这一点?
PS:在我的例子中,Fire AJAX
将调用更新数据库中记录的操作控制器方法,因此我也想知道关于安全问题我应该\必须了解什么。
I am using Ruby on Rails 3 and I am trying to improve an AJAX request in a view file using the remote_function
method. The following code will display the text Fire AJAX
when the mouse pointer is over the <div id="test_id_div">...</div>
and will call an action controller method on clicking on the Fire AJAX
.
In the view file I have:
<div id="test_id_div">
<span id="test_id_span" %> style="display: none">
<%= link_to_function 'Fire AJAX', remote_function(:url => {:action => :action_method_name, :controller => 'controller_name'}) %>
</span>
<%=
update_page_tag do |page|
page.event.observe("test_id_div", 'mouseover') do |element|
element[("test_id_span").to_sym].show
end
page.event.observe("test_id_div", 'mouseout') do |element|
element[("test_id_span").to_sym].hide
end
end
%>
</div>
The above code works, however I would like to improve that in some "better" way (example: writing less code, ...). How could I do that?
P.S.: In my case the Fire AJAX
will call an action controller method that update a record in the database, so I would like to know what I should\must know about security matter too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mouseover/out
效果是怎样的,这可以通过一些 CSS 来实现(IE6 不支持):ERB:
CSS:
我既没有使用 Prototype 也没有使用相关的助手,但我相当确定您应该能够使用
link_to... :remote =>; true
在这种情况下也是如此,因为你似乎没有调用本地 JS 函数:并且进一步简化事情:如果你想简单地隐藏/显示链接,为什么不摆脱一个容器?
我希望我没有在某个地方误解了你,但这应该可以解决问题。
What comes to the
mouseover/out
effect, this could be achieved with a dash of CSS (not supported by IE6):ERB:
CSS:
I'm neither using Prototype nor the related helpers, but I'm fairly sure that you should be able to use
link_to… :remote => true
in this case as well, since you don't seem to be calling a local JS function:And to simplify things even further: if you want to simply hide/show the link, why not get rid of one container?
I hope I've not misunderstood you somewhere, but this should do the trick.