我試圖通過Rails4中的「用戶」控制器中的「更新」動作來更新用戶表中的用戶數據。我正在使用PostgreSQL數據庫。使用Rails4中的update_attributes更新表單數據
問題:當我去「編輯」行動,我得到正確呈現的形式。在填寫表格中的一列或多列數據後,當我按下「更新配置文件」按鈕更新用戶數據時,然後形成頁面重定向並返回到相同的編輯頁面,但現在URL已更改。數據未保存在數據庫中。它僅在表單域中顯示。即使配置文件圖片正在上傳,但其鏈接並未保存在數據庫中。
例子:
更新之前 - >網址:http://localhost:3000/users/edit?id=2
後更新 - > URL:http://localhost:3000/users/update/2
但新輸入的數據並沒有得到保存在數據庫中。我也檢查了軌道服務器日誌,但我不明白,表單有什麼問題。通過rails控制檯,我嘗試更新用戶的數據,但仍然無法將數據保存到數據庫中。請檢查截圖。
用戶控制器:
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 %>
我認爲:
- 也許在「更新」操作中有錯誤。但是如果出現任何錯誤,那麼Rails必須在處理表單時發生錯誤。
- 受保護的方法「user_params」可能會保護數據不被提交。由於我沒有填寫所有表格欄。我在1到多個表單域中輸入數據。
- 在編輯表單中可能有錯誤。
用戶控制器,帶有註冊操作:
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 %>
不,沒有工作。 –
未經許可的參數是一個問題,但可能不是唯一的參數。在更改後立即查看服務器日誌會很有幫助。下一個線索就在那裏。請將日誌發佈到您的原始問題。 – Elvn
日誌始終相同。但表單數據不會保存。我也在表單中使用了一些禁用的字段,可能它們導致了一些問題。但我仍然知道別的東西正在造成問題。我正在上傳一個新的服務器日誌,pease check。 –