我在尝试注册时遇到了困难。当我点击“注册”按钮后填写new.html.erb的所有字段后,我收到的错误是这样的:enter image description here 点击注册后,它呈现相同的页面,但是有错误列表并且全部为空。我无法弄清楚它为什么不保存而另一件事就是为什么它会给出这些错误,如果我是填补整个领域。
这是我的程序
new.html.erb
<%= form_for @user, :html=> {:id => 'create_user'} do |f| %>
<div class="text-center" style="font-size:16px;color:red;">
<% if @user.errors.any? %>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
</div>
<div class="form-group">
<%= f.text_field :name, class: 'form-control', placeholder: 'Enter Name' , name: "name" , id: "name" %>
</div>
<div class="form-group">
<%= f.text_field :category, list: "category", id:"select", placeholder:"Select By category" %>
<datalist id="category">
<select>
<option value="Student" />
<option value="Faculty" />
<option value="HOD" />
<option value="Principle" />
<option value="Dean" />
<option value="Chairman" />
</select>
</datalist>
</div>
<div class="form-group">
<%= f.email_field :email, class: 'form-control', placeholder: 'Email' , name:"email" , id: "email" %>
</div>
<div class="form-group">
<%= f.password_field :password, class: 'form-control', placeholder: 'Password' , name: "password" , id:"password" %>
</div>
<div class="form-group">
<%= f.password_field :password_confirmation , placeholder: "Re-type Password" , class: "form-control" , name:"password_confirmation", id:"Re_type" %>
</div>
<div class="form-group">
<div class="row" style="margin-top:20px;">
<div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-10 col-xs-offset-1">
<%= f.submit "Register", class: "btn btn-primary btn-block disabled" , id: "register" %>
</div>
</div>
</div>
<% end %>
的routes.rb
Rails.application.routes.draw do
root "users#index"
get '/' => "users#index"
get "/new" => "users#new"
resources :users
post "/users" => "users#create"
get "users/:id" => "users#show"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
users_controller.rb
Class UsersController < ApplicationController
def index
end
def new
@user = User.new
end
def show
end
def create
@user = User.create(user_params)
puts "======================================================================"
puts @user
puts "======================================================================"
if @user.save
session[:user_id] = @user.id
flash[:success] = "Welcome to the My project!"
redirect_to user_path(:id)
else
render :new1
end
end
private
def user_params
params.require(:user).permit(:name,:category, :email, :password)
end
end
user.rb
class User
include Mongoid::Document
field :name, type: String
field :category, type: String
field :email, type: String
field :password, type: String
validates :name, presence: true ,format: { with: /\A[a-zA-Z]+\z/}
validates :email, presence: true , format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i } , uniqueness: { case_sensitive: false }
validates :password , presence: true
before_save :downcase_fields
def downcase_fields
self.name.downcase!
self.category.downcase!
self.email.downcase!
end
end
请帮帮我。 我使用mongodb作为后端并尝试存储表单字段中的数据,但它始终显示错误。它既不会将有效数据存储到数据库中。
答案 0 :(得分:0)
您的错误会覆盖您输入的name
和id
。主要是name
。
<%= f.email_field :email, class: 'form-control', placeholder: 'Email' , name: "email" , id: "email" %>
^^^^^^^^^^^^^^^^^^^^^^^^^^^
删除它们,它应该工作。
说明:按rails惯例,在这种情况下,名称应为user[name]
,ID应为user_name
。
因为名称不是user[name]
,所以强大的params方法会丢弃它。它希望name
嵌套在user
params.require(:user).permit(:name)