ruby-on-rails - ruby 名称不能为空错误

标签 ruby-on-rails forms devise

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

注册页面时出现“姓名不能为空”、“手机号不能为空”的错误,尽管我填写了姓名和手机号

我的routes.rb是

Rails.application.routes.draw do
 devise_for :users
 namespace :api do
  resources :users, :defaults => { :format => 'json' }
 end 

我的 Controller 页面

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"}

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

谢谢大家的回复

关于ruby-on-rails - ruby 名称不能为空错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30422997/

相关文章:

javascript - Rails link_to with AJAX 禁用特定链接的 AJAX 加载覆盖

PHP MySQL 多个 id 插入并设置名称

ruby-on-rails - Rails + Devise HTTP header token

ruby-on-rails - URL 中的参数未传递给表单 - Rails 4

ruby-on-rails - 用户模型声明性授权 'if_attribute'问题

mysql - 在 Rails 中指定连接表的条件

ruby-on-rails - PostgreSQL 检查时间戳字段是否为空

php - 用于更改 true 与 false 函数值的复选框

javascript - 在提交表单之前对输入标签执行验证

ruby-on-rails - 有没有办法将 "before_filter :authenticate_user!"与表中的 bool 值一起使用?