改进“remote_function”使用“高级”阿贾克斯

发布于 2024-10-27 15:20:26 字数 1007 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里南柯 2024-11-03 15:20:26

mouseover/out 效果是怎样的,这可以通过一些 CSS 来实现(IE6 不支持):

ERB:

<div id="test_id_div">
  <span id="test_id_span" %>>
    <%= link_to_function 'Fire AJAX', remote_function(:url => {:action => :action_method_name, :controller => 'controller_name'}) %>
  </span>
</div>

CSS:

#test_id_div span { display: none; }
#test_id_div:hover span { display: block; }

我既没有使用 Prototype 也没有使用相关的助手,但我相当确定您应该能够使用 link_to... :remote =>; true 在这种情况下也是如此,因为你似乎没有调用本地 JS 函数:

<%= link_to 'Fire AJAX', :action => :action_method_name, :controller => 'controller_name', :remote => true %>

并且进一步简化事情:如果你想简单地隐藏/显示链接,为什么不摆脱一个容器?

<div id="test_id_div">
  <%= link_to 'Fire AJAX', :action => :action_method_name, :controller => 'controller_name', :remote => true %>
</div>

#test_id_div a { display: none; }
#test_id_div:hover a { display: block; }

我希望我没有在某个地方误解了你,但这应该可以解决问题。

What comes to the mouseover/out effect, this could be achieved with a dash of CSS (not supported by IE6):

ERB:

<div id="test_id_div">
  <span id="test_id_span" %>>
    <%= link_to_function 'Fire AJAX', remote_function(:url => {:action => :action_method_name, :controller => 'controller_name'}) %>
  </span>
</div>

CSS:

#test_id_div span { display: none; }
#test_id_div:hover span { display: block; }

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:

<%= link_to 'Fire AJAX', :action => :action_method_name, :controller => 'controller_name', :remote => true %>

And to simplify things even further: if you want to simply hide/show the link, why not get rid of one container?

<div id="test_id_div">
  <%= link_to 'Fire AJAX', :action => :action_method_name, :controller => 'controller_name', :remote => true %>
</div>

#test_id_div a { display: none; }
#test_id_div:hover a { display: block; }

I hope I've not misunderstood you somewhere, but this should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文