在x可编辑保存后重新渲染部分

时间:2014-01-22 20:58:23

标签: javascript ruby-on-rails ajax x-editable

我使用x-editable来允许用户将自己与另一个模型相关联。样式根据用户所关联的模型类型而变化。

现在,它可以工作,但css类只在刷新页面后才会更改。

在x-editable保存新值后,我将如何重置css类?

这就是我输入的样子 - >

== editable user, :heat_id, url: admin_user_path(user), type: "select", value: (user.heat.level if user.heat), source: Heat.all.map{ | heat | {value: heat.id, text: heat.level} }, class: "#{user.heat.badge if user.heat}"

我基本上需要重新申请

class: "#{user.heat.badge if user.heat}"

更新

我发现x-editable-rails实际上内置了这个。解决方案是写 - >

== editable user, :heat_id, url: admin_user_path(user), type: "select", value: (user.heat.level if user.heat), source: Heat.all.map{ | heat | {value: heat.id, text: heat.level} }, classes: Hash[Heat.all.map{ | heat | [heat.id, heat.badge]}], class: "#{user.heat.badge if user.heat}"

但是,我仍然不知道如何在使用ajax保存后更改元素。

例如,我想做的是,在保存事件之后,重新渲染部分“进度”。

我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

$('.profile_editable').on('save', function(e, params) {
    var progress_value = params.response.progress_value
    $('.progress_title').text("Your profile is " + progress_value + "% completed" )
    $('.bar').css('width', progress_value + '%');
});

然后,在json响应中传递信息 - >

  def update
    @common_app = CommonApp.find(params[:id])
    if @common_app.update_attributes(common_app_params)
      respond_to do |format|
        format.html { render :action => "show" }
        format.json { render json: {success: true, progress_value: @common_app.user.progress, user_name: @common_app.user.name } }
      end
    else
       render json: {errors: @common_app.errors}, status: 400
    end
  end
相关问题