由于前一篇文章中的格式错误而重新发布。
我收到以下错误,我在下面为整个MVC添加了代码。这里奇怪的是控制器想要包含“标题”方法但我在其他任何地方都没有标题要求,这可能是指其他东西。虽然这不起作用,但def更新方法不会修补,因此我无法将任何用户更新或新数据保存到系统中(这是一个HRIS系统btw)。
所以错误是;
NoMethodError in UsersController#update
undefined method `title' for #<User:0x007ff3dd9f8a70>
Extracted source (around line #56):
54
55
56
57
58
59
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Information Updated"
redirect_to @user
#handle successful update
这是我的架构;
create_table "users", force: :cascade do |t|
t.string "name"
t.string "employee"
t.string "email"
t.string "password"
t.date "startdate"
t.date "probationcomp"
t.date "annualreview"
t.text "address"
t.string "phonenumber"
t.string "nextofkin"
t.string "kinnumber"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.boolean "admin", default: false
t.string "leavenum"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.boolean "hstrain"
t.boolean "induction"
t.boolean "lhtrain"
t.boolean "vodatotal"
t.boolean "commtrainops"
t.boolean "commtrainsell"
t.boolean "hosttrainops"
t.boolean "hosttrainsell"
t.boolean "cpstrainops"
t.boolean "cpstops"
t.boolean "cpstsell"
t.string "leaverem"
t.text "specialism"
t.string "shelf"
t.string "doc_file_name"
t.string "doc_content_type"
t.integer "doc_file_size"
t.datetime "doc_updated_at"
end
观点;
<h1>Change your profile settings</h1>
<div class="row">
<%= form_for @user do |f| %>
<%= render 'shared/error_messages' %>
<!-- left side -->
<div class="col-md-6">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.label :address, "Home Address" %>
<%= f.text_area :address, :cols => "1", :rows => "5", class: "form-control" %>
</div>
<!-- right side -->
<div class="col-md-6">
<%= f.label :phonenumber, "Contact Number" %>
<%= f.text_field :phonenumber, class: "form-control" %>
<%= f.label :nextofkin, "Emergency Contact Person" %>
<%= f.text_field :nextofkin, class: "form-control" %>
<%= f.label :kinnumber, "Emergency Contact Phone Number" %>
<%= f.text_field :kinnumber, class: "form-control" %>
<div class="gravatar_edit">
<%= gravatar_for @user %>
<a href="https://gravatar.com/emails" target=_blank>Change Profile Pic</a>
</div><br><p><br>
<%= f.submit "Save Changes", class: 'btn btn-warning' %>
<% end %>
</div>
</div>
控制器(这是我认为错误的地方,虽然更新功能看起来很好,我没有发布整个控制器,但如果有帮助可以做)
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Information Updated"
redirect_to @user
#handle successful update
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :employee, :email, :password, :startdate, :probationcomp,
:password_confirmation, :annualreview, :address, :phonenumber, :nextofkin, :kinnumber,
:created_at, :updated_at, :password_digest, :admin, :leavenum, :avatar_file_name, :avatar_content_type, :avatar_file_size,
:avatar_updated_at, :hstrain, :induction, :lhtrain, :vodatotal, :commtrainops, :commtrainsell, :hosttrainops,
:hosttrainsell, :cpstrainops, :cpstops, :cpstsell, :leaverem, :specialism, :shelf, :doc_file_name, :doc_content_type,
:doc_file_size, :doc_updated_at)
end
最后是模型;
class User < ApplicationRecord
attr_accessor :remember_token
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 7 }, allow_nil: true
#set up "uploaded_file" field as attached_file (using Paperclip)
searchable do
text :name, :id, :phonenumber
end
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::password.create(string, cost: cost)
end
def User.new_token
SecureRandom.urlsafe_base64
end
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
def authenticated?
return false if remember_digest.nil?
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
def forget
update_attribute(:remember_token, nil)
end
end
我确定之前有人遇到过这个问题,只是看不到木头的树木。测试脚本返回时出现同样的错误。