我正在使用best_in_place编辑使用'devise'管理的配置文件数据库中的字段。编辑视图中的所有内容都可以正常更新,但在使用best_in_place编辑单个字段后不久,该字段将返回其原始值,并且数据库尚未更新。
注意:运行bundle install,重新启动服务器以及刷新页面都没有用。
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)
的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显示:
//= require jquery
//= require jquery_ujs
//= require jquery.purr
//= require best_in_place
//= require_tree .
提前感谢您的帮助。
答案 0 :(得分: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
修改强>
嗯,可能是设计期望密码更新用户,这导致回滚,因为它找不到。您可以执行以下两项操作之一:从用户模型中的设计中删除:validatable
,或将这两个验证覆盖添加到您的用户模型validates :password, presence: true, length: {minimum: 5, maximum: 120}, on: :create
validates :password, length: {minimum: 5, maximum: 120}, on: :update, allow_blank: true