0
我試圖在rails 5中從頭開始進行身份驗證,並且我的用戶信息在輸入註冊表單時沒有被保存。我也收到此錯誤軌道:::加載ActiveModel ForbiddenAttributesError如何將我的用戶信息從註冊表單保存到數據庫?
@user = User.new(PARAMS [:用戶])
class User < ApplicationRecord
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :email
validates_uniqueness_of :email
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password,
user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password,
password_salt)
end
end
end
<h1>Sign Up</h1>
<%= form_for @user do |f| %>
<% if @user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in @user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<p class="button"><%= f.submit %></p>
<% end %>
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
end
class Post < ApplicationRecord
has_secure_password
end
非常感謝!所以對於鐵軌來說很陌生,並試圖將我的頭包裹在它周圍。 –
如果它幫助和糾正你的問題,你可以通過檢查我的答案旁邊的標記來接受我的答案,所以這個問題標記爲已解決 – widjajayd
這有幫助,但我也需要刪除attr_accessor。再次感謝:) –