1

我使用 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 保存后更改元素。

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

我该怎么做呢?

4

1 回答 1

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
于 2014-01-28T07:05:47.420 回答