gpt4 book ai didi

ruby-on-rails - 在 Rails4 中使用 update_attributes 不更新表单数据

转载 作者:行者123 更新时间:2023-11-29 13:55:43 25 4
gpt4 key购买 nike

我正在尝试通过在 Rails4 的“用户” Controller 中使用“更新”操作来更新用户表中的用户数据。我正在使用 PostgreSQL 数据库。

问题:当我进入“编辑”操作时,我得到了正确呈现的表单。在表单中的 1 列或多列中填写数据后,当我按“更新配置文件”按钮更新用户数据时,表单页面重定向并返回到相同的编辑页面,但现在 url 已更改。数据未保存在数据库中。它仅显示在表单字段中。即使是个人资料图片也在上传,但它们的链接没有保存在数据库中。

示例:

更新前 -> URL:http://localhost:3000/users/edit?id=2

更新后 -> URL:http://localhost:3000/users/update/2

但是新输入的数据并没有保存在数据库中。我还检查了 rails server 日志,但我不明白,表单有什么问题。通过 Rails 控制台,我尝试更新用户的数据,但我仍然无法将数据保存在数据库中。请检查屏幕截图。

Rails 服务器日志: enter image description here

Rails 控制台: enter image description here

用户 Controller :

    class UsersController < ApplicationController
def edit
@user = User.find("#{@current_user_id}")
end

def update
User.transaction do
@user = User.find(params[:id]).lock!
if @user.update_attributes(user_params)
Welcome.profile_data_updated(@user).deliver_now
redirect_to(:controller => 'users', :action => 'profile', :id => @current_user_id)
else
render('edit')
end
end
end
protected
def user_params
params.require(:user).permit(:username, :user_image, :first_name, :last_name, :password, :email, :street_address, :city, :state, :country, :postal_code, :mobile_number)
end
end

CreateUsers 迁移文件:

class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username, null: false
t.string :user_image
t.string :first_name, null: false
t.string :last_name, null: false
t.string :password_digest, null: false
t.string :email, null: false
t.string :street_address
t.string :city
t.string :state
t.string :country
t.string :postal_code
t.string :mobile_number
t.string :validation_code
t.string :user_status, null: false, :default => '1' # 0 for deactivated
t.timestamps null: false
end
end

def down
drop_table :users
end
end

用户模型:

class User < ActiveRecord::Base
has_secure_password
validates_confirmation_of :password

include CarrierWave::RMagick
mount_uploader :user_image, ImageUploader


validates_presence_of :username, :format => {:with => /\A[a-zA-Z0-9]+\Z/, :message => 'Username can only contain Alphabets'}
validates_presence_of :first_name,:last_name,:password,:email
validates_uniqueness_of :username, :email
validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :on => :create
validates_numericality_of :postal_code, :mobile_number, :on => :update

def full_name
"#{first_name} #{last_name}"
end

def location
"#{street_address}, #{city}, #{state}, #{country}, #{postal_code}"
end
end

Update.html.erb 文件:

<%= form_for @user, url: { action: "update", :id => @user.id }, method: :post, html: { multipart: true, class: "ui form"} do |f| %>
<div align="center"><%= image_tag @user.user_image.to_s, :size => "200x200" %></div>
</br>
<div align="center">
<%= f.file_field :user_image %>
</div>

<h4 class="ui dividing header">Billing Information</h4>
<div class="field">
<%= f.label "Name" %>

<div class="three fields">
<div class="field">
<%= f.text_field :username, :placeholder => 'Username' %>
</div>
<div class="field">
<%= f.text_field :first_name, :placeholder => 'First Name' %>
</div>
<div class="field">
<%= f.text_field :last_name, :placeholder => 'Last Name' %>
</div>
</div>
</div>

<div class="field">
<%= f.label "Billing Address" %>
<div class="fields">
<div class="ten wide field">
<%= f.text_field :street_address, :placeholder => 'Street Address' %>
</div>
<div class="three wide field">
<%= f.text_field :city, :placeholder => 'City' %>
</div>
<div class="three wide field">
<%= f.text_field :postal_code, :placeholder => 'Postal Code' %>
</div>
</div>
</div>

<div class="two fields">
<div class="field">
<%= f.label "State" %>
<%= f.text_field :state, :placeholder => 'State' %>
</div>
<div class="field">
<%= f.label "Country" %>
<%= f.text_field :country, :placeholder => 'Country' %>
</div>
</div>

<h4 class="ui dividing header">Contact Information</h4>
<div class="two fields">
<div class="disabled field">
<%= f.label "Email" %>
<%= f.text_field :email, :disabled => true %>
</div>
<div class="field">
<%= f.label "Contact" %>
<%= f.text_field :mobile_number, :placeholder => 'Mobile Number' %>
</div>
</div>

<h4 class="ui dividing header">Security</h4>
<div class="two fields">
<div class="field">
<%= f.label "New Password" %>
<%= f.password_field :password, :placeholder => 'New Password' %>
</div>
<div class="field">
<%= f.label "Retype Password" %>
<%= f.password_field :password_confirmation, :placeholder => 'Retype Password' %>
</div>
</div>
<div>
<%= f.submit "Update Profile", data: { disable_with: "Updating..." }, class: 'ui green button pull-right'%>
</div>
<% end %>

我的意见:

  1. 也许“更新”操作有误。但是,如果出现任何错误,那么 Rails 一定是在处理表单时抛出了错误。
  2. protected 方法“user_params”可能会保护数据不被提交。因为我没有填写所有表格栏。我在 1 到多个表单字段中输入数据。
  3. 编辑形式可能有错误。

新服务器日志: enter image description here

具有注册操作的用户 Controller :

def signup
@user = User.new
end

def create
@user = User.new(user_params)
if @user.save
Welcome.welcome(@user).deliver_now
redirect_to(:controller => 'access', :action => 'login')
else
render('signup')
end
end

注册表单:

<%= form_for(:user, :html => {:multipart => true }, :url => {:controller => 'users',:action => 'create'})  do |f| %>

<h3 class="nomargin">Sign Up</h3>
<p class="mt5 mb20">Already a member? <%= link_to "Login" , {:controller => 'access', :action => 'login'} %><br><br>

<label class="control-label">Username</label>
<%= f.text_field :username, class: 'form-control' ,placeholder:'Username' %></br>

<label class="control-label">Email</label>
<%= f.text_field :email, class: 'form-control' ,placeholder:'Email' %></br>

<label class="control-label">Profile Picture</label>
<%= f.file_field(:user_image) %></br>

<label class="control-label">Name</label>
<div class="row mb10">
<div class="col-sm-6">
<%= f.text_field :first_name, class: 'form-control', placeholder: 'First Name' %></br>
</div>
<div class="col-sm-6">
<%= f.text_field :last_name, class: 'form-control', placeholder: 'Last Name' %></br>
</div>
</div>

<div class="mb10">
<label class="control-label">Password</label>
<%= f.password_field :password, class: 'form-control', placeholder: 'Password' %></br>
</div>

<div class="mb10">
<label class="control-label">Retype Password</label>
<%= f.password_field :password_confirmation, class: 'form-control', placeholder: 'Retype Password' %></br>
</div>

<button class="btn btn-success btn-block">Sign Up</button>
<% end %>

最佳答案

您发布的第一个日志中的错误表明更新时使用了不允许的参数 => :password_confirmation。

请尝试将 :password_confirmation 添加到您的强参数验证中。

def user_params
params.require(:user).permit(:username, :user_image, :first_name, :last_name, :password, :email, :street_address, :city, :state, :country, :postal_code, :mobile_number, :password_confirmation)
end

请告诉我进展如何。

strong 参数更改后的更新部分包括表单字段 password_confirmation 和讨论

请尝试更改此设置:

    class UsersController < ApplicationController
def edit
@user = User.find("#{@current_user_id}")
end

对此:

      class UsersController < ApplicationController 
# Precursor action to rendering the edit user view
def edit
@user = User.find(params[:id])
end

关于ruby-on-rails - 在 Rails4 中使用 update_attributes 不更新表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32423121/

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