Rails 5枚举,选择字段不保存

时间:2018-04-24 02:14:17

标签: ruby-on-rails enums

我知道这已被多次询问,但由于某些原因,其他解决方案都没有对我有用。我仍然无法在更新时保存带枚举的字段。当我使用相同的表单传递非枚举值(如first_name)时,它会更新,但枚举值(性别)永远不会保存,即使它显示它是......

查看:

<%= form_for(@patient, url: patient_path(params[:id])) do |f| %>
    <%= f.fields_for :role do |r| %>
        <%= r.fields_for :user do |u| %>
        <div class="keyvalue">
            <div class="key"><p>Gender</p></div>
            <div class="value">
                <%= u.select :gender, User.genders.map { |key, value| [key.humanize, key] }, include_blank: '-Select A Gender-' %>
            </div>
            <div class="push"></div>
        </div><!--keyvalue-->
        <% end %>
    <% end %>
<% end %>

控制器:

def update
    @patient = Patient.find(params[:id])
    if @patient.update(patient_params)
        flash[:success] = "Patient's record has been updated."
        redirect_to patient_path(params[:id])
    else
        flash[:error] = @patient.errors.full_messages.to_sentence
        redirect_to patient_path(params[:id])
    end
end

日志:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"*********", "commit"=>"Update", "patient"=>{"role_attributes"=>{"user_attributes"=>{"first_name"=>"John", "middle_name"=>"Joseph", "last_name"=>"Doe", "date_of_birth"=>"1943-12-25", "gender"=>"male", "blood_type"=>"", "ethnicity"=>"", "marital_status"=>"", "id"=>"1"}, "id"=>"1"}}, "id"=>"1"}
  Patient Load (0.1ms)  SELECT  "patients".* FROM "patients" WHERE "patients"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
   (0.0ms)  begin transaction
  Role Load (0.1ms)  SELECT  "roles".* FROM "roles" WHERE "roles"."roleable_id" = ? AND "roles"."roleable_type" = ? LIMIT ?  [["roleable_id", 1], ["roleable_type", "Patient"], ["LIMIT", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  User Exists (0.1ms)  SELECT  1 AS one FROM "users" WHERE "users"."username" = ? AND ("users"."id" != ?) LIMIT ?  [["username", "dev"], ["id", 1], ["LIMIT", 1]]
  SQL (0.3ms)  UPDATE "users" SET "gender" = ?, "updated_at" = ? WHERE "users"."id" = ?  [["gender", 0], ["updated_at", "2018-04-24 02:11:23.126843"], ["id", 1]]
   (2.6ms)  commit transaction
Redirected to http://localhost:3000/patients/1
Completed 302 Found in 10ms (ActiveRecord: 3.2ms)

用户模型中的枚举:

enum gender: [:male, :female, :non_binary, :rather_not_say]

迁移:

t.integer :gender

日志显示没有问题,它显示正在更新的性别字段,但如果我转到rails控制台,则会将性别显示为nil

0 个答案:

没有答案