除了电子邮件ID和密码之外,我没有在数据库中获取设计值

时间:2014-01-24 09:48:13

标签: ruby-on-rails devise sqlite

我已经在设计中输入了另外三个字段,然后通过电子邮件发送密码进行注册。当我点击注册后输入数据时,它将获取空值并显示错误消息。如果我删除验证然后输入数据然后它接受注册,但是当我在数据库中看到它显示空值时。它只需要电子邮件密码

我的应用程序控制器是

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
   before_filter :update_sanitized_params, if: :devise_controller?

   def update_sanitized_params
     devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:name, :surname, :age)}
   end
 end

我的模型控制器是

class Twk < ActiveRecord::Base
   # Include default devise modules. Others available are:
   # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable

 validates_presence_of :name, :message => "is must."
   validates_presence_of :age, :message => "is needed.", value:  {minimum: 1}

      end

我该怎么办请建议..

我的表格

<h2>Sign up</h2>

 <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
   <%= devise_error_messages! %>


   <div><%= f.label :name %><br />
  <%= f.text_field :name %></div>
    <div><%= f.label :surname %><br />
    <%= f.text_field :surname %></div>
   <div><%= f.label :age %><br />
    <%= f.number_field :age %></div>
  <div><%= f.label :email %><br />
       <%= f.email_field :email, :autofocus => true %></div>

      <div><%= f.label :password %><br />
       <%= f.password_field :password %></div>

     <div><%= f.label :password_confirmation %><br />
       <%= f.password_field :password_confirmation %></div>

        <div><%= f.submit "Sign up" %></div>
         <% end %>

      <%= render "devise/shared/links" %>

最后我的links.erb是

<%- if controller_name != 'sessions' %>
  <%= link_to "Sign in", new_session_path(resource_name) %><br />
<% end -%>

<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
  <%= link_to "Sign up", new_registration_path(resource_name) %><br />
<% end -%>

  <%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
     <%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
   <% end -%>

    <%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
            <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
      <% end -%>

     <%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
          <%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br />
       <% end -%>

     <%- if devise_mapping.omniauthable? %>
      <%- resource_class.omniauth_providers.each do |provider| %>
           <%= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider) %><br />
        <% end -%>
     <% end -%>

1 个答案:

答案 0 :(得分:0)

您是否检查了日志中的Unpermitted parameters消息?

如果要使用{|u| u.permit(:name, :surname, :age)},则需要指定每个参数,而不仅仅是其他参数。如果您想添加其他参数而不将其全部列出,则可以使用devise_parameter_sanitizer.for(:sign_up) << :name等格式。