2017-05-07 56 views
0

我在嘗試註冊時遇到困難。填寫new.html.erb的所有字段後,當我點擊註冊按鈕時,出現如下錯誤:enter image description here After點擊寄存器時,呈現在同一頁面,但有錯誤列表和使所有的empty.I不能能夠弄清楚爲什麼它不保存和另一件事就是爲什麼它是給那些錯誤,如果我填整個領域。在存儲表單數據時面臨的問題

這裏是我的程序

new.html.erb

<%= form_for @user, :html=> {:id => 'create_user'} do |f| %> 
    <div class="text-center" style="font-size:16px;color:red;"> 
    <% if @user.errors.any? %> 
     <ul> 
      <% @user.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
      <% end %> 
     </ul> 
     <% end %> 
    </div> 

    <div class="form-group"> 
    <%= f.text_field :name, class: 'form-control', placeholder: 'Enter Name' , name: "name" , id: "name" %> 

    </div> 
    <div class="form-group"> 
    <%= f.text_field :category, list: "category", id:"select", placeholder:"Select By category" %> 
     <datalist id="category"> 
     <select> 
      <option value="Student" /> 
      <option value="Faculty" /> 
      <option value="HOD" /> 
      <option value="Principle" /> 
      <option value="Dean" /> 
      <option value="Chairman" /> 
     </select> 
     </datalist> 
    </div> 
    <div class="form-group"> 
    <%= f.email_field :email, class: 'form-control', placeholder: 'Email' , name:"email" , id: "email" %> 
    </div> 
    <div class="form-group"> 
    <%= f.password_field :password, class: 'form-control', placeholder: 'Password' , name: "password" , id:"password" %> 
    </div> 
    <div class="form-group"> 
    <%= f.password_field :password_confirmation , placeholder: "Re-type Password" , class: "form-control" , name:"password_confirmation", id:"Re_type" %> 
    </div> 
    <div class="form-group"> 
    <div class="row" style="margin-top:20px;"> 
     <div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-10 col-xs-offset-1"> 
     <%= f.submit "Register", class: "btn btn-primary btn-block disabled" , id: "register" %> 
     </div> 
    </div> 
    </div> 
<% end %> 

的routes.rb

Rails.application.routes.draw do 
    root "users#index" 
    get '/' => "users#index" 
    get "/new" => "users#new" 
    resources :users 
    post "/users" => "users#create" 
    get "users/:id" => "users#show" 

    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
end 

users_controller.rb

Class UsersController < ApplicationController 
    def index 
    end 

    def new 
    @user = User.new 
    end 

    def show 
    end 

    def create 
    @user = User.create(user_params) 
    puts "======================================================================" 
    puts @user 
    puts "======================================================================" 
    if @user.save 
     session[:user_id] = @user.id 
     flash[:success] = "Welcome to the My project!" 
     redirect_to user_path(:id) 
    else 
     render :new1 
    end 
    end 

    private 

    def user_params 
     params.require(:user).permit(:name,:category, :email, :password) 
    end 
end 

user.rb

class User 
    include Mongoid::Document 

    field :name, type: String 
    field :category, type: String 
    field :email, type: String 
    field :password, type: String 
    validates :name, presence: true ,format: { with: /\A[a-zA-Z]+\z/} 
    validates :email, presence: true , format: { with: /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i } , uniqueness: { case_sensitive: false } 
    validates :password , presence: true 
    before_save :downcase_fields 

    def downcase_fields 
    self.name.downcase! 
    self.category.downcase! 
    self.email.downcase! 
    end 
end 

請幫助我。 我使用mongodb作爲後端,並試圖存儲表單字段的數據,但它始終顯示錯誤。它既沒有將有效數據存儲到數據庫中。

+0

什麼是錯誤?請將其添加到您的問題。 – Gerry

+0

格里,我做了更改後,我也附含error.Please幫助我解決這個問題的圖像。 –

+0

你有什麼'params'? –

回答

0

你的錯誤是覆蓋在你的輸入nameid。主要是name

<%= f.email_field :email, class: 'form-control', placeholder: 'Email' , name: "email" , id: "email" %> 
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

丟棄這些,它應該工作。


說明:由軌道約定,在這種情況下,名稱應該是user[name]和ID應該是user_name

因爲名字是不是user[name]你的堅強PARAMS方法將其丟棄。它預計name嵌套在user

params.require(:user).permit(:name)