处理注册失败失败

时间:2017-04-17 05:13:46

标签: html ruby-on-rails ruby

之前我确实创建了一个名为address的属性。但我没有在注册表单中使用它。当我在注册表单中输入所有文本字段时,它会闪烁错误

  

表单包含1个错误。

     

地址不能为空

我应该检查的其他地方?

控制器

 def new
        @tutor = Tutor.new
      end
      def create
        @tutor = Tutor.new(tutor_params)
        if @tutor.save
          log_in @tutor
          flash[:success] = "Congratulations! Your registration is successful!"
          redirect_to @tutor
        else
          render 'tutors/new'
        end
      end
    # Handle sign-up failure, to redirect the tutor to the registeration form again
      def tutor_params
        params.require(:tutor).permit(:name, :email, :password, :password_confirmation,:gender 
          ,:education_level,:institution,:exprience,:district,:subject,:student_level)
      end

注册页面

     <%= form_for(@tutor) do |f| %>
    <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name, class: 'form-control' %>

      <%= f.label :email %>
      <%= f.text_field :email, class: 'form-control' %>

      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>

      <%= f.label :password_confirmation, "Confirm Password" %>
      <%= f.password_field :password_confirmation, class: 'form-control' %>



 <%= f.label :gender %>
            <%= f.select(:gender, ['Male', 'Female'] , class: 'form-control' )%>


      <%= f.label :tutor_education_level %>
                <%= f.select(:education_level, ['Bachelor', 'Master', 'Doctor'] , class: 'form-control' )%>


      <%= f.label :tutor_institution %>
      <%= f.text_field :institution, class: 'form-control' %>

            <%= f.label :tutorial_experience %>
      <%= f.text_field :experience, class: 'form-control' %>

            <%= f.label :tutor_preferred_district %>
      <%= f.text_field :district, class: 'form-control' %>

      <%= f.label :tutor_preferred_subject %>
      <%= f.text_field :subject, class: 'form-control' %>

            <%= f.label :tutor_required_student_level %>

       <%= f.select(:student_level, ['P1-P3', 'P4-P6', 'S1-S3', 'S4-S6'] , class: 'form-control' )%>


      <%= f.submit "Create tutor's account", class: "btn btn-primary" %>

    <% end %>

_error_messages.html.erb

<% if @tutor.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(@tutor.errors.count, "error") %>.
    </div>
    <ul>
    <% @tutor.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

的db / schema.rb(uupdate)

  create_table "tutors", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "password_digest"
    t.string   "address"
    t.string   "remember_token"
    t.string   "gender"
    t.string   "education_level"
    t.string   "institution"
    t.integer  "experience"
    t.string   "district"
    t.string   "subject"
    t.string   "student_level"
  end

  add_index "tutors", ["email"], name: "index_tutors_on_email", unique: true
  add_index "tutors", ["remember_token"], name: "index_tutors_on_remember_token"

end

tutor.rb(update2)

`class Tutor < ActiveRecord::Base
    before_save {self.email = email.downcase}
    before_save :create_remember_token
    validates :name, presence: true, length: { maximum: 50}
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
    validates :password, presence: true, length: { minimum: 6 }
    validates :password_confirmation, presence: true
    has_secure_password
    validates :address, presence: true, length: {maximum: 100}

  def create_remember_token
    self.remember_token = SecureRandom.urlsafe_base64
  end
end
`

2 个答案:

答案 0 :(得分:0)

def create
        @tutor = Tutor.new(tutor_params)
        if @tutor.save
          log_in @tutor
          flash[:success] = "Congratulations! Your registration is successful!"
          redirect_to @tutor
        else
          flash[:tutor] = @tutor
          redirect_to new_tutor_path
        end
      end

还要确保您的导师模型中的validates_presence_of:地址。

答案 1 :(得分:0)

更改

validates :address, presence: true, length: {maximum: 100}

validates :address, length: {maximum: 100}

在您的模型中tutor.rb

您在创建导师时验证模型中是否存在地址字段,但您没有传递地址参数。

如果要从db中删除现有地址字段, 在您的终端中,执行

rails g migration remove_address_from_tutors

在新创建的迁移文件中添加以下代码

class RemoveAddressFromTutors < ActiveRecord::Migration
  def change
    remove_column :tutors, :address
  end
end

然后在终端中执行rake db:migrate