2016-09-18 64 views
0

我試圖挽救輪廓更新時間轉換每當禁止選項被選中到整數,但每當我看着數據庫字段,ban_time爲零。無法保存當前時間在Rails的模型

def update 
    find_user 

    if !params[:banned].nil? 
     @user.ban_time = Time.now.to_i 
     @user.save 
    end 

    if @user.update(user_params) 
     redirect_to @user, notice: "Profil je uspešno ažuriran." 
    else 
     render "edit" 
    end 
end 

其他所有內容均正確保存。

更新:這裏是用戶的模式和模型。

模式

create_table "users", force: :cascade do |t| 
    t.string "username" 
    t.string "email" 
    t.string "password_digest" 
    t.datetime "created_at",   null: false 
    t.datetime "updated_at",   null: false 
    t.string "location" 
    t.date  "birthdate" 
    t.string "rank" 
    t.boolean "banned" 
    t.text  "bandesc" 
    t.boolean "vip" 
    t.integer "ban_time" 
    t.integer "ban_remaining_days" 
    t.integer "bug_report_time" 
    end 

型號

class User < ApplicationRecord 

    has_secure_password 

    validates :username, presence: true, uniqueness: true, format: { with: /\A[a-zA-Z0-9]+\Z/}, 
          length: { maximum: 10, minimum: 4 } 
    validates :email, presence: true, uniqueness: true, length: { maximum: 50 } 
    validates :password, presence: true, length: { maximum: 50, minimum: 4 }, if: :should_validate_pass? 
    validates :password_confirmation, presence: true, if: :should_validate_conf? 
    validates :location, presence: true, length: { maximum: 30 } 
    validates :banned, presence: true, if: :ban_remaining_days? 
    validates :ban_remaining_days, presence: true, numericality: { only_integer: true }, inclusion: { in: 1..365 }, if: :banned? 
    validates :bandesc, presence: true, length: { maximum: 200, minimum: 20 }, if: :banned? 

    def should_validate_pass? 
     new_record? || password.present? 
    end 

    def should_validate_conf? 
     new_record? || password_confirmation.present? 
    end 

end 

<%= form_for @user do |f| %> 
    <% if @user.errors.any? %> 
     <div class="alert alert-danger"> 
      <ul> 
       <% @user.errors.full_messages.each do |m| %> 
        <li><%= m %></li> 
       <% end %> 
      </ul> 
     </div> 
    <% end %> 

    <div class="field"> 
     <%= f.label "Korisničko ime:" %> 
     <%= f.text_field :username, class: "form-control" %> 
    </div> 

    <div class="field"> 
     <%= f.label "Lokacija:" %> 
     <%= f.text_field :location, class: "form-control" %> 
    </div> 

    <div class="field"> 
     <%= f.label "Datum rođenja:" %><br/> 
     <%= f.date_select :birthdate, order: [:day, :month, :year], start_year: 1900, end_year: Time.now.year - 18 %> 
    </div> 

    <div class="field"> 
     <%= f.label "E-mail:" %> 
     <%= f.text_field :email, class: "form-control" %> 
    </div> 

    <div class="field"> 
     <%= f.label "Lozinka:" %> 
     <%= f.password_field :password, class: "form-control" %> 
    </div> 

    <div class="field"> 
     <%= f.label "Potvrda lozinke:" %> 
     <%= f.password_field :password_confirmation, class: "form-control" %> 
    </div> 

     <h3>Administratorska podešavanja</h3> 

     <div class="field"> 
      <%= f.label "Rank:" %> 
      <%= f.text_field :rank, class: "form-control" %> 
     </div> 

     <div class="field"> 
      <%= f.label "VIP:" %> 
      <%= f.check_box :vip %> 
     </div> 

     <div class="field"> 
      <%= f.label "Banovanje člana:" %> 
      <%= f.check_box :banned %> 
     </div> 

     <div class="field"> 
      <%= f.label "Razlog:" %> 
      <%= f.text_area :bandesc, class: "form-control" %> 
     </div> 

     <div class="field"> 
      <%= f.label "Vremenski period (dana):" %> 
      <%= f.text_field :ban_remaining_days, class: "form-control" %> 
     </div> 

    <div class="submit"> 
     <%= f.submit button_text, class: "btn btn-primary" %> 
    </div> 

<% end %> 
+0

你能爲用戶顯示模型和模式嗎? –

+0

@JohnFeltz問題已更新。 – Nikola

+0

謝謝。放入一個調試點並驗證params散列是否正確。 –

回答

2
 <%= f.check_box :banned %> 

...將返回 '0' false或 '1',如果true

而且也沒有params[:banned]這是params[:user][:banned]user_params[:banned]

因此改變

if !params[:banned].nil? 

if user_params[:banned] == '0' 
+0

謝謝!我在一篇文章中看到,在這個SO上,未檢查的複選框甚至沒有發送(但是當我看到請求輸出時意識到它是錯誤的),所以我認爲檢查零將做這項工作。 – Nikola

相關問題