Ruby on rails 未使用Rails4中的update_属性更新表单数据
我试图通过在Rails4中的“用户”控制器中使用“更新”操作来更新用户表中的用户数据。我正在使用PostgreSQL数据库 问题:当我转到“编辑”操作时,我得到了正确呈现的表单。在表单中填充1列或多列数据后,当我按“更新配置文件”按钮更新用户数据时,表单页面重定向并返回到同一编辑页面,但现在url已更改。数据未保存在数据库中。它仅在表单字段中显示。即使是个人资料图片也在上传,但它们的链接没有保存在数据库中 示例: 更新前->URL: 更新->URL后: 但是新输入的数据没有保存在数据库中。我还检查了rails服务器日志,但我不明白表单有什么问题。通过rails控制台,我试图更新用户的数据,但仍然无法将数据保存到数据库中。请检查屏幕截图 Rails服务器日志: Rails控制台: 用户控制器:Ruby on rails 未使用Rails4中的update_属性更新表单数据,ruby-on-rails,forms,postgresql,ruby-on-rails-4,postgresql-9.3,Ruby On Rails,Forms,Postgresql,Ruby On Rails 4,Postgresql 9.3,我试图通过在Rails4中的“用户”控制器中使用“更新”操作来更新用户表中的用户数据。我正在使用PostgreSQL数据库 问题:当我转到“编辑”操作时,我得到了正确呈现的表单。在表单中填充1列或多列数据后,当我按“更新配置文件”按钮更新用户数据时,表单页面重定向并返回到同一编辑页面,但现在url已更改。数据未保存在数据库中。它仅在表单字段中显示。即使是个人资料图片也在上传,但它们的链接没有保存在数据库中 示例: 更新前->URL: 更新->URL后: 但是新输入的数据没有保存在数据库中。我还检
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
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
<%= 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 %>
报名表格:
<%= 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 %>
{:multipart=>true},:url=>{:controller=>'users',:action=>'create'})do | f |%>
注册
已经是会员了吗访问“,:操作=>'login'}%>
用户名
电子邮件
侧面图
名称
密码
重新输入密码
注册
您发布的第一个日志中的错误表明更新时有一个不允许的参数=>:密码\u确认
请尝试将:密码\确认添加到强参数验证中
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参数更改后更新的部分包括表单字段密码\u确认和讨论
请尝试更改此选项:
class UsersController < ApplicationController
def edit
@user = User.find("#{@current_user_id}")
end
class UsersController
为此:
class UsersController < ApplicationController
# Precursor action to rendering the edit user view
def edit
@user = User.find(params[:id])
end
class UsersController
您发布的第一个日志中的错误表明更新时有一个不允许的参数=>:密码\u确认
请尝试将:密码\确认添加到强参数验证中
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参数更改后更新的部分包括表单字段密码\u确认和讨论
请尝试更改此选项:
class UsersController < ApplicationController
def edit
@user = User.find("#{@current_user_id}")
end
class UsersController
为此:
class UsersController < ApplicationController
# Precursor action to rendering the edit user view
def edit
@user = User.find(params[:id])
end
class UsersController
unpermitted参数是个问题,但可能不是唯一的问题。更改后现在查看服务器日志会很有帮助。下一条线索就在那里。请把日志贴到你原来的问题上。日志总是一样的。但是表单数据不保存。我还在表单中使用了一些禁用的字段,可能它们导致了一些问题。但我仍然知道是其他原因造成了问题。我正在上传一个新的服务器日志,请检查。我这里有线索。当我使用“更新_属性”时,数据没有提交到数据库中。但是当我使用“update_attribute”时,数据被保存到了数据库中。是的。谢谢。我看到了新的日志。根据提交状态200,更新已成功提交给ID为3的用户。您认为哪些记录没有更新?什么是不存在的行为/数据?每次我都收到提交成功的消息,但该提交从未在数据库中发生。这就是问题所在。未授权参数是一个问题,但可能不是唯一的问题。更改后现在查看服务器日志会很有帮助。下一条线索就在那里。请把日志贴到你原来的问题上。日志总是一样的。但是表单数据不保存。我还在表单中使用了一些禁用的字段,可能它们导致了一些问题。但我仍然知道是其他原因造成了问题。我正在上传一个新的服务器日志,请检查。我这里有线索。当我使用“更新_属性”时,数据没有提交到数据库中。但是当我使用“update_attribute”时,数据被保存到了数据库中。是的。谢谢。我看到了新的日志。根据提交状态200,更新已成功提交给ID为3的用户。您认为哪些记录没有更新?什么是不存在的行为/数据?每次我都收到提交成功的消息,但该提交从未在数据库中发生。这就是问题所在。