【发布时间】:2016-09-18 22:05:12
【问题描述】:
每当检查 canned 选项时,我试图节省转换为整数编号的配置文件的时间,但是每当我查看数据库字段时, ban_time nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil。 /p>
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 %>
【问题讨论】:
-
你能显示用户的模型和架构吗?
-
@JohnFeltz 问题已更新。
-
谢谢。放入调试点并验证 params 哈希是否正确。
-
为什么不使用日期时间字段?
-
你能显示定义复选框的视图吗?显然,它并没有像您期望的那样通过视图->控制器。
标签: ruby-on-rails