gpt4 book ai didi

ruby-on-rails-4 - Rails 4 - best_in_place : Unprocessable Entity

转载 作者:行者123 更新时间:2023-12-04 07:16:23 26 4
gpt4 key购买 nike

我正在使用 best_in_place 编辑由“devise”管理的配置文件数据库中的字段。编辑 View 中的所有内容都更新正常,但在使用 best_in_place 编辑单个字段后不久,该字段返回到其原始值,并且数据库没有更新。

注意:运行 bundle install、重新启动服务器和刷新页面均无效。

用户 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

用户.js.咖啡:

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 文件:

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 .

提前感谢您的帮助。

最佳答案

尝试替换:

    format.json { head :ok }

与:

    format.json { respond_with_bip(@user) }

在 Controller 中的 respond_to block 中。

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

编辑:

嗯,可能是设计需要密码来更新用户,这导致回滚,因为它找不到密码。您可以执行以下两项操作之一:从 User 模型的设计中删除 :validatable,或者将这两个验证覆盖添加到您的 User 模型,validates :password, presence: true, length: {最小值:5,最大值:120},关于::创建 验证:密码,长度:{最小值:5,最大值:120},关于::更新,allow_blank:true/p>

关于ruby-on-rails-4 - Rails 4 - best_in_place : Unprocessable Entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21436394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com