Question

I'm using best_in_place to edit a field in a profile database managed with 'devise'. Everything updates fine in the edit view, but shortly after editing a single field with best_in_place the field returns to its original value, and the database has not been updated.

NOTE: Running bundle install, restarting the server, and refreshing the page have not worked.

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()

users/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 error console shows:

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

Server log shows:

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)

Gemfile:

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 shows:

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

Thank you in advance for any help.

Was it helpful?

Solution

Try replacing:

    format.json { head :ok }

with:

    format.json { respond_with_bip(@user) }

in your respond_to block in your controller.

respond_with_bip is a "utility method you should use in your controller in order to provide the response that is expected by the javascript side, using the :json format" https://github.com/bernat/best_in_place

EDIT:

Hmm, it might be that devise is expecting a password to update the user, and this is causing the rollback, since it can't find one. You can do one of two things: remove :validatable from devise in your User model, or add these two validation overrides to your User model, validates :password, presence: true, length: {minimum: 5, maximum: 120}, on: :create validates :password, length: {minimum: 5, maximum: 120}, on: :update, allow_blank: true

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top