ruby 名称不能为空错误
ruby name can't be blank error
这可能是一个重复的问题,但以前的答案对我没有帮助
我在注册页面时遇到 "name can't be blank"、"mobile no cann't be blank" 错误,即使我填写了姓名和手机号码
我的routes.rb是
Rails.application.routes.draw do
devise_for :users
namespace :api do
resources :users, :defaults => { :format => 'json' }
end
我的控制器页面
class RegistrationsController < Devise::RegistrationsController
skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }
def new
@user = User.new
end
def create
params.permit!
@user = User.new(params[:user])
@user = User.new
@user.name = params[:user][:name]
@user.email = params[:user][:email]
@user.mobile_no = params[:user][:mobile_no]
@user.password = params[:user][:password]
@user.valid?
if@user.errors.blank?
@user.save
else
render :action => "new"
end
end
def update
super
end
end
我的浏览页是
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :mobile_no %><br />
<%= f.telephone_field :mobile_no, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @validatable %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
我的模特是
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def users_params
params.require(:users).permit(:name, :email, :mobile_no, :password)
end
validates :email,:name, :mobile_no :presence =>true
validates_uniqueness_of :email
end
我错过了什么,...提前致谢....
问题出在我的 routes.rb
替换了路线,从
devise_for :users
到,
devise_for :users, :controllers => {:registrations => "registrations"}
现在,一切正常,...
感谢大家的回复
这可能是一个重复的问题,但以前的答案对我没有帮助
我在注册页面时遇到 "name can't be blank"、"mobile no cann't be blank" 错误,即使我填写了姓名和手机号码
我的routes.rb是
Rails.application.routes.draw do
devise_for :users
namespace :api do
resources :users, :defaults => { :format => 'json' }
end
我的控制器页面
class RegistrationsController < Devise::RegistrationsController
skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }
def new
@user = User.new
end
def create
params.permit!
@user = User.new(params[:user])
@user = User.new
@user.name = params[:user][:name]
@user.email = params[:user][:email]
@user.mobile_no = params[:user][:mobile_no]
@user.password = params[:user][:password]
@user.valid?
if@user.errors.blank?
@user.save
else
render :action => "new"
end
end
def update
super
end
end
我的浏览页是
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :mobile_no %><br />
<%= f.telephone_field :mobile_no, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @validatable %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
我的模特是
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def users_params
params.require(:users).permit(:name, :email, :mobile_no, :password)
end
validates :email,:name, :mobile_no :presence =>true
validates_uniqueness_of :email
end
我错过了什么,...提前致谢....
问题出在我的 routes.rb
替换了路线,从
devise_for :users
到,
devise_for :users, :controllers => {:registrations => "registrations"}
现在,一切正常,...
感谢大家的回复