3

我正在使用 best_in_place 编辑使用“设计”管理的配置文件数据库中的字段。编辑视图中的所有内容都可以正常更新,但在使用 best_in_place 编辑单个字段后不久,该字段将返回其原始值,并且数据库尚未更新。

注意:运行捆绑安装、重新启动服务器和刷新页面都不起作用。

users_controller.rb:

class UsersController < ApplicationController    
respond_to :html, :json

...

    def update
    @user = User.find_by_id(params[:id])

        respond_to do |format|
        if @user.update_attributes(user_params)
            format.html { redirect_to(@user, :notice => 'Profiiile was successfully updated.') }
            format.json { head :ok }
        else
            format.html { render :action => "show" }
            format.json { render :json => @user.errors.full_messages, :status => :unprocessable_entity }
            end
        end
    end

...

private
    def user_params
        params.require(:user).permit(
            :id,
            :first,
            :last,
            :email,
            :password,
            :password_confirmation,
            :remember_me,
            :user_phone,
            :user_birthday,
            :user_gender,
            :user_street_address,
            :user_city,
            :user_state,
        )
    end
end

users.js.coffee:

jQuery ->
    $('.best_in_place').best_in_place()

用户/show.html.erb

<section id="header">
    <div class="row title">
        <div class="large-12 column">
            <h1><%= best_in_place @user, :first %> <%= best_in_place @user, :last %></h1>
        </div>
    </div>
</section>

Javascript 错误控制台显示:

09:00:12.367 POST http://localhost:3000/users/10 [HTTP/1.1 422   67ms]

服务器日志显示:

Started PUT "/users/10" for 127.0.0.1 at 2014-01-29 09:00:12 -0700
Processing by UsersController#update as JSON
Parameters: {"user"=>{"email"=>"asteel@gmail.com"},     "authenticity_token"=>"GzAlA00LQlvdPTPmKtbLcl38/TLgpJg2P0jRpuydTc8=", "id"=>"10"}
User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 10 ORDER BY users.first DESC LIMIT 1
(0.3ms)  BEGIN
User Exists (0.8ms)  SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'astel@gmail.com' AND "users"."id" != 10) LIMIT 1
(0.3ms)  ROLLBACK
Completed 422 Unprocessable Entity in 34ms (Views: 0.1ms | ActiveRecord: 2.4ms)

宝石文件:

gem 'rails', '4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
gem 'bootstrap-sass', '2.0.0'
gem "font-awesome-rails"
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.0.1'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
gem 'pg'
gem 'devise'
gem "best_in_place", github: 'bernat/best_in_place', branch: "rails-4"

Application.js 显示:

//= require jquery
//= require jquery_ujs
//= require jquery.purr
//= require best_in_place
//= require_tree .

预先感谢您的任何帮助。

4

1 回答 1

6

尝试更换:

    format.json { head :ok }

和:

    format.json { respond_with_bip(@user) }

在您的控制器中的 respond_to 块中。

respond_with_bip 是一种“您应该在控制器中使用的实用方法,以便使用 :json 格式提供 javascript 端所期望的响应” https://github.com/bernat/best_in_place

编辑:

嗯,可能是 devise 需要一个密码来更新用户,这导致了回滚,因为它找不到密码。您可以做以下两件事之一::validatable从您的用户模型中的设计中删除,或者将这两个验证覆盖添加到您的用户模型中,validates :password, presence: true, length: {minimum: 5, maximum: 120}, on: :create validates :password, length: {minimum: 5, maximum: 120}, on: :update, allow_blank: true

于 2014-02-03T20:41:59.260 回答