Rails 3 就地编辑

发布于 2024-10-01 13:47:42 字数 1791 浏览 7 评论 0原文

我正在尝试在 Rails 3 应用程序中实现就地编辑,该应用程序获取编辑后的值并根据该值修改计算。例如:

编辑“捐赠积分”字段之前:
您还剩 10 个学分。捐赠积分:0。

编辑“捐赠积分”字段后:
您还剩 5 个学分。捐赠积分:5。

我研究了各种选项,看起来 jquery-in-place-editor 可以满足我的要求。供参考:

该演示有效。但我认为我需要做的是使用回调函数来调用ruby脚本,这样我就可以做更多的处理。我不完全确定这是否是我需要做的,所以我正在寻找一些指导。

这是 jQuery javascript (来自 /public/javascripts/application.js 在示例应用程序中),我认为是相关的:

// Using a callback function to update 2 divs
$("#editme4").editInPlace({
    callback: function(original_element, html, original){
        $("#updateDiv1").html("The original html was: " + original);
        $("#updateDiv2").html("The updated text is: " + html);
        return(html);
    }
});

看起来我需要做的是采用 originalhtml 并尝试一下它们,然后将它们反馈给适当的div。问题是,我该怎么做?我正在考虑两种可能性:

A. jquery-in-place-editor 中有一个工具可以调用 url。我可以用它来调用 ruby​​ 脚本并获得结果吗?我该如何在 Rails 中做到这一点?下面是调用 url 的代码(同样来自 /public/javascripts/application.js);但它调用了一个php脚本。

$("#editme1").editInPlace({
    // callback: function(unused, enteredText) { return enteredText; },
    url: 'server.php',
    show_buttons: true
});

B.直接在javascript中修改更新的文本,并将其传递给适当的div。我更喜欢使用 ruby​​ 脚本,但这也许会更容易。

I'm trying to implement in-place editing in a Rails 3 app that takes the edited value and revises a calculation based on that value. For example:

Before edit of the "credits to donate" field:
You have 10 credits remaining. Credits to donate: 0.

After edit of the "credits to donate" field:
You have 5 credits remaining. Credits to donate: 5.

I have looked into various options, and it looks like the jquery-in-place-editor does what I'm looking for. For reference:

The demo works. But I think what I need to do is use the callback function to call a ruby script, so I can do some more processing. I'm not entirely sure if that's what I need to do though, so I'm looking for some guidance.

Here is the jQuery javascript (from /public/javascripts/application.js in the Sample app) that I think is relevant:

// Using a callback function to update 2 divs
$("#editme4").editInPlace({
    callback: function(original_element, html, original){
        $("#updateDiv1").html("The original html was: " + original);
        $("#updateDiv2").html("The updated text is: " + html);
        return(html);
    }
});

It looks like what I need to do is take original and html and play around with them a bit, then feed them back to the appropriate divs. The question is, how do I do this? I'm thinking of two possibilities:

A. There is a facility in the jquery-in-place-editor to call a url. Could I use this to call a ruby script and get a result? How would I do this in rails? Here is code to call a url (again from /public/javascripts/application.js); but it calls a php script.

$("#editme1").editInPlace({
    // callback: function(unused, enteredText) { return enteredText; },
    url: 'server.php',
    show_buttons: true
});

B. Modify the updated text directly in javascript, and pass it to the appropriate divs. I'd prefer to use a ruby script, but maybe this would be easier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

鸠魁 2024-10-08 13:47:42

这个问题的解决方案相当复杂,但我找到了一种方法。令我惊讶的是,该解决方案并不容易获得,因为正如 r-dub 所说,它确实看起来很常见。我最终没有使用 jQuery 插件的任何就地编辑功能,而是在表单文本字段的基础上借助 jQuery 直接修改文本字段。换句话说,我遵循了上面的(B)。

我有一个看起来像这样的视图:

Available: <span id="available_cr"><%= @available_credits %></span>
Remaining: <span id="remaining_cr"><%= @remaining_credits %></span>

<%= form_for(@page, :html => { :id => 'do_fvalue' }, :remote => true) do |f| %>
  <div class="field">
    How many to buy at <%= @cost %> credits per unit? <%= f.text_field :fvalue, :autocomplete => :off %>
  </div>
  <div class="field">
    How many to buy at <%= @cost_20 %> credits per unit? <%= f.text_field :gvalue, :autocomplete => :off %>
  </div>
  <div class="actions">
    <%= f.submit "Update" %>
  </div>
<% end %>

然后我将 Rails @-variables 填充到 .html.erb 部分(在我的页面 .html.erb 的开头呈现),以便 javascript/jQuery 可以访问它们:

<script type="text/javascript">
  var cost = parseInt(<%= @cost %>)
  var cost_20 = parseInt(<%= @cost_20 %>)
  var available_credits = parseInt(<%= @available_credits %>)
  var remaining_credits = parseInt(<%= @remaining_credits %>)
</script>

然后,在公共场合/javascripts/applications.js,我有以下代码:

function checkFields(){
  var to_buy_f = $("#page_fvalue").val();
  var to_buy_g = $("#page_gvalue").val();
  var subtract_f = to_buy_f * cost;
  var subtract_g = to_buy_g * cost_20;
  var remain = available_credits - subtract_f - subtract_g;
  $("#remaining_cr").text(remain);
};

$("#page_fvalue").keyup(function () {
   checkFields();
}).keyup();

$("#page_gvalue").keyup(function () {
   checkFields();
}).keyup();

checkFields 函数根据 #page_fvalue 和 #page_gvalue 中找到的值更新 #remaining_cr。请注意,它使用了 available_credits、cost 和 cost_20,我们将其填充到上面的 javascript 变量中。最后两个函数告诉 jQuery 监视 #page_fvalue 和 #page_gvalue 的 keyup 事件。当用户按下某个键时,它会调用 checkFields() 来更新#remaining_cr。

除此之外,当用户单击表单上的“提交”时,整个表单都会以当前值提交。我不知道这是否是做我想做的事情最有效的方法,但它有效!

The solution to this problem is quite complex, but I have found a way to do it. It is surprising to me that the solution is not readily available, since it does seem, as r-dub says, something that is quite common. I ended up not using any of the edit-in-place facilities of a jQuery plugin, and instead modified a text field directly with the help of jQuery on the basis of a form text field. I other words, I followed (B) above.

I have a view that looks like this:

Available: <span id="available_cr"><%= @available_credits %></span>
Remaining: <span id="remaining_cr"><%= @remaining_credits %></span>

<%= form_for(@page, :html => { :id => 'do_fvalue' }, :remote => true) do |f| %>
  <div class="field">
    How many to buy at <%= @cost %> credits per unit? <%= f.text_field :fvalue, :autocomplete => :off %>
  </div>
  <div class="field">
    How many to buy at <%= @cost_20 %> credits per unit? <%= f.text_field :gvalue, :autocomplete => :off %>
  </div>
  <div class="actions">
    <%= f.submit "Update" %>
  </div>
<% end %>

I then stuff the rails @-variables into a .html.erb partial (rendered at the beginning of my page .html.erb) so that javascript/jQuery can access them:

<script type="text/javascript">
  var cost = parseInt(<%= @cost %>)
  var cost_20 = parseInt(<%= @cost_20 %>)
  var available_credits = parseInt(<%= @available_credits %>)
  var remaining_credits = parseInt(<%= @remaining_credits %>)
</script>

Then, in public/javascripts/applications.js, I have the following code:

function checkFields(){
  var to_buy_f = $("#page_fvalue").val();
  var to_buy_g = $("#page_gvalue").val();
  var subtract_f = to_buy_f * cost;
  var subtract_g = to_buy_g * cost_20;
  var remain = available_credits - subtract_f - subtract_g;
  $("#remaining_cr").text(remain);
};

$("#page_fvalue").keyup(function () {
   checkFields();
}).keyup();

$("#page_gvalue").keyup(function () {
   checkFields();
}).keyup();

The checkFields function updates #remaining_cr based on the values found in #page_fvalue and #page_gvalue. Notice that it makes use of available_credits, cost, and cost_20, which we stuffed into the javascript variables above. The last two functions tell jQuery to monitor #page_fvalue and #page_gvalue for keyup events. When the user presses a key, it calls checkFields() to update #remaining_cr.

All of this aside, when the user clicks "submit" on the form, the whole form gets submitted with the current values. I don't know whether this is the most efficient way to do what I was trying to do, but it works!

峩卟喜欢 2024-10-08 13:47:42

你想做的事情很常见。您想要做的是在该回调中发出 AJAX 请求,将原始数据发送到您的 Rails 应用程序并将修改后的 html 返回到您的页面。这是 jQuery AJAX 手册页。向下滚动查看一些示例。

What you're trying to do is quite common. What you want to do is make an AJAX request within that callback sending the original data to your rails app and returning the modified html to your page. Here's the jQuery AJAX manual page. Scroll way down to see some examples.

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