2016-11-09 68 views
0

我在用户注册期间收到nomethod名称错误。但是,数据库中的用户表中没有名称列,并且表单中没有名称字段,所以我不明白为什么会引发此错误。这是错误,然后是我的表单,控制器和模式。 noMethodError in Users#Create (wtf?)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 
+0

请将您'user_params'方法。 – araratan

+0

并且还添加你的'app/models/user.rb'文件 –

+0

这里是我的模型: –

回答

0

去检查你的模型,你在验证现场

代码进行错误

validates :name, presence: true, length: { maximum: 50 }` 

改变喜欢这个

validates :first_name, :last_name, presence: true, length: { maximum: 50 } 
+0

谢谢!哇,我无法弄清楚“名字”是从哪里来的。我花了一个多小时来搜索我的表格,模式等,而且一定忽略了它。 Rails错误也不是很有帮助,但非常感谢你! –

+0

欢迎@ FrederickJohn – prasanthrubyist

+0

如果你对我的回答感到高兴,你可以投票吗? – prasanthrubyist

0

你不应该验证:姓名与您的用户模型,因为你不具备的:名字列在其中。