我在用戶註冊期間收到nomethod名稱錯誤。但是,數據庫中的用戶表中沒有名稱列,並且表單中沒有名稱字段,所以我不明白爲什麼會引發此錯誤。這是錯誤,然後是我的表單,控制器和模式。 Rails 5'nomethod'在註冊期間出錯
<%= form_for(user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :display_name %>
<%= f.text_field :display_name %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
這裏是我的用戶控制器。該錯誤源自用戶控制器創建操作。
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
當然,我認爲它可能是DB架構,但它不是。這是架構。
ActiveRecord::Schema.define(version: 20161109225217) do
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "email"
t.string "display_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
end
end
我不知道什麼是造成這個錯誤。請提供任何建議!?
class User < ApplicationRecord
attr_accessor :password
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end
而且我的用戶PARAMS -
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :display_name, :password, :password_confirmation)
end
end
請將您'user_params'方法。 – araratan
並且還添加你的'app/models/user.rb'文件 –
這裏是我的模型: –