ruby名称不能为空白错误

时间:2015-05-24 11:28:42

标签: ruby-on-rails forms devise

这可能是一个重复的问题,但之前的答案对我没有帮助

我在注册页面时得到“名字不能为空”,“手机不能空白”错误,即使我填写了名称和手机没有

我的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

我错过了什么,...提前感谢....

1 个答案:

答案 0 :(得分:1)

问题在于我的routes.rb

替换了路线
devise_for :users

要,

devise_for :users, :controllers => {:registrations => "registrations"}

现在,它工作正常,......

感谢各位回复