我使用Devise来设置例程用户模块,但后来我想添加更多变量并遇到麻烦。我跟着this tutorial进行了以下更改:
routes
的{{1}}行现在显示为:
devise
我的devise_for :users, :controllers => { registrations: 'registrations' }
是:
registrations_controller
我以其他人的方式将字段添加到class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :role, :gender, :dob, :age, :goal_weight, :goal_muscle, :goal_bodyimage, :goal_performance, :goal_endurance, :goal_health, :email, :password, :password_confirmation)
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :role, :gender, :dob, :age, :goal_weight, :goal_muscle, :goal_bodyimage, :goal_performance, :goal_endurance, :goal_health, :email, :password, :password_confirmation, :current_password)
end
end
和new
个视图中。
这是我的edit
表格:
new
这是我的<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field text-left">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true, class: "form-control" %>
</div>
<div class="field text-left">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, class: "form-control" %>
</div>
<div class="field text-left">
<%= f.label :gender %><br />
<%= f.select :gender, options_for_select([['Prefer Not to Say', 'Prefer Not to Say'],['Female', 'Female'],['Male', 'Male']]), {}, {class: "form-control"} %>
</div>
<div class="field text-left">
<%= f.label :dob %><br />
<%= f.date_field :dob, class: "form-control" %>
</div>
<div class="field text-left">
<%= f.label "What are your goals?" %><br />
<p><%= f.check_box :goal_weight, value: "1", checked: false, options: {} %> Lose Weight<br>
<%= f.check_box :goal_muscle, value: "1", checked: false, options: {} %> Gain Muscle<br>
<%= f.check_box :goal_bodyimage, value: "1", checked: false, options: {} %> Better Overall Body Composition<br>
<%= f.check_box :goal_performance, value: "1", checked: false, options: {} %> Increase Sports Performance<br>
<%= f.check_box :goal_endurance, value: "1", checked: false, options: {} %> Gain Endurance<br>
<%= f.check_box :goal_health, value: "1", checked: false, options: {} %> Better Health</p>
</div>
<div class="field text-left">
<%= f.label :email %><br />
<%= f.email_field :email, class: "form-control" %>
</div>
<div class="field text-left">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
</div>
<div class="field text-left">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
</div>
<div class="actions">
<%= f.submit "Sign up", class: "btn btn-primary" %>
</div>
<% end %>
表格:
edit
所有表单都能正确呈现。当我点击<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true, class: "form-control" %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, class: "form-control" %>
</div>
<div class="field">
<%= f.label :gender %><br />
<%= f.select :gender, options_for_select([['Prefer Not to Say', 'Prefer Not to Say'],['Female', 'Female'],['Male', 'Male']]), {}, {class: "form-control"} %>
</div>
<div class="field">
<%= f.label :dob %><br />
<%= f.date_field :dob, class: "form-control" %>
</div>
<div class="field">
<%= f.label "What are your goals?" %><br />
<p><%= f.check_box :goal_weight, value: "1", checked: false, options: {} %> Lose Weight<br>
<%= f.check_box :goal_muscle, value: "1", checked: false, options: {} %> Gain Muscle<br>
<%= f.check_box :goal_bodyimage, value: "1", checked: false, options: {} %> Better Overall Body Composition<br>
<%= f.check_box :goal_performance, value: "1", checked: false, options: {} %> Increase Sports Performance<br>
<%= f.check_box :goal_endurance, value: "1", checked: false, options: {} %> Gain Endurance<br>
<%= f.check_box :goal_health, value: "1", checked: false, options: {} %> Better Health</p>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, class: "form-control", autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, class: "form-control", autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, class: "form-control", autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, class: "form-control", autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update My Info", class: "btn btn-primary" %>
</div>
<% end %>
或submit
时,我会收到闪存通知,表示信息已成功保存。
当我保存信息时,我在服务器中得到类似的内容,表明正在保存新信息,包括复选框值:
update
麻烦在哪里开始。当我回到我的Started PUT "/users" for ::1 at 2016-06-03 17:17:42 -0700
Processing by RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"DomUJ8aFghDIOPYsWQPlB7KnmFt27ZDcPOf7SdG4PWvI8QgI3OZP5mscbVfpGtlNpIZKTIqhL+RRwj1/3VT9AQ==", "user"=>{"first_name"=>"Test", "last_name"=>"Testerson", "gender"=>"Prefer Not to Say", "dob"=>"2008-08-08", "goal_weight"=>"0", "goal_muscle"=>"1", "goal_bodyimage"=>"0", "goal_performance"=>"1", "goal_endurance"=>"0", "goal_health"=>"0", "email"=>"test@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update My Info"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
(0.1ms) begin transaction
SQL (0.4ms) UPDATE "users" SET "goal_weight" = ?, "goal_performance" = ?, "updated_at" = ? WHERE "users"."id" = ? [["goal_weight", 0], ["goal_performance", 1], ["updated_at", "2016-06-04 00:17:43.005855"], ["id", 2]]
(0.6ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 148ms (ActiveRecord: 1.2ms)
页面之后,除了复选框之外,所有新变量都显示在它们的字段中(无论先前检查了多少/几个:
然后,当我在控制台中检查用户时,会显示NONE的新变量:
edit
另一方面,我 CAN 使用irb(main):007:0> User.second
User Load (7.5ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 OFFSET 1
=> #<User id: 2, email: "test@gmail.com", encrypted_password: "$2a$11$y9/F8GQ9sTUK7Hm.b81Eo.VEbvvZ93XdOdZivAgQxhB...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2016-06-04 00:04:34", last_sign_in_at: "2016-06-04 00:04:34", current_sign_in_ip: "::1", last_sign_in_ip: "::1", created_at: "2016-06-04 00:04:34", updated_at: "2016-06-04 00:17:43">
之类的erb在其他地方调用它们。
任何人都可以帮我解决这个问题,以便保存所有新变量吗?
其他信息:
以下是将新变量添加到<%= @user.first_name %>
模型的迁移:
user
答案 0 :(得分:0)
从所有复选框中删除checked:false。这应该可以解决复选框问题。例如:
<%= f.check_box :goal_weight, value: "1", options: {} %> Lose Weight<br>
更新:users表确实有自定义字段列 - 正如您在评论中提到的那样(但在问题控制台输出中未提及),在用户的末尾有一个省略号(...
)在控制台输出中。这意味着还有更多字段。