加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在json更新后,Rails3用js响应

发布时间:2020-12-17 03:00:14 所属栏目:百科 来源:网络整理
导读:使用 http://blog.bernatfarrero.com/in-place-editing-with-javascript-jquery-and-rails-3/ gem使用json进行更新,但是如何触发update.js.erb来更新页面的不同部分? 编辑 在发票页面中使用它.发票中的每个项目都有一个可以使用best_in_place更新的价格字段
使用 http://blog.bernatfarrero.com/in-place-editing-with-javascript-jquery-and-rails-3/

gem使用json进行更新,但是如何触发update.js.erb来更新页面的不同部分?

编辑
在发票页面中使用它.发票中的每个项目都有一个可以使用best_in_place更新的价格字段.

只有在字段成功更新后,我才需要更新订单项的总价格和发票到期金额.

结束了类似的事情:

respond_to do |format|
        if @item.update_attributes(params[:item])
          format.html { redirect_to order_url(@item.order_id),:notice => "Successfully updated item." }
          format.js {  }
          format.json { head :ok }

编辑best_in_place.js第175行

loadSuccessCallback : function(data) {
    this.element.html(data[this.objectName]);
        // Binding back after being clicked
    $(this.activator).bind('click',{editor: this},this.clickHandler);
    if(this.objectName == "item") $.getScript('update.js'); // If its an item,call update.js.erb 
  },

解决方法

您不需要查看update.js.erb.正如ezkl已经指出的那样,你需要将控制器的respond_to设置为json.此外,您需要在public / javascripts / application.js中激活最佳位置

$(document).ready(function() {
  jQuery(".best_in_place").best_in_place()
});

并将其添加到您的视图中:

<td><%= best_in_place task,:name,:type => :input,:nil => "Click to edit" %></td>

我在自己的网站上有一个关于Ajax和Prototype和JQuery的教程 – 而JQuery Part使用最好的就地:http://www.communityguides.eu/articles/15

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读