我正在使用Devise处理所有会话信息和用户身份验证 - 但我想为管理员添加编辑用户个人资料信息(姓名,地址等)的功能
我创建了一个user_controller,允许管理员更新其他配置文件的用户信息。所有表单都正确显示,当管理员编辑用户信息时,会显示“用户已成功更新” - 但信息实际上并未更新到数据库。
以下是我的users_controller中的相关方法:
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy] # probably want to keep using this
# GET /users/1/edit
def edit
unless current_user && current_user.admin?
redirect_to root_path, notice: "Must be signed in as Admin"
end
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
以下是表格:
<%= form_for(user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :address %>
<%= f.text_field :address %>
</div>
<div class="field">
<%= f.label :city %>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :state %>
<%= f.text_field :state %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我觉得好像我必须遗漏一些明显的东西,但我似乎无法绕过这个足以找到它。
提前感谢您提供任何帮助 - 如果我遗漏了需要的东西以获得更好的照片,请告诉我!
编辑:
以下是提交更新表格时的输出:
Started PATCH "/users/5" for 73.104.86.133 at 2017-07-16 12:16:02 +0000
Cannot render console from 73.104.86.133! Allowed networks: 127.0.0.1, ::1,
127.0.0.0/127.255.255.255
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"**truncated**", "user"=>{"first_name"=>"Tests",
"last_name"=>"Accounts", "address"=>"12345 Main St", "city"=>"Atlantis",
"state"=>"FL"}, "commit"=>"Update User", "id"=>"5"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
Unpermitted parameters: first_name, last_name, address, city, state
(0.1ms) begin transaction
(0.0ms) commit transaction
Redirected to **truncated**/users/5
Completed 302 Found in 7ms (ActiveRecord: 0.3ms)
Started GET "/users/5" for 73.104.86.133 at 2017-07-16 12:16:02 +0000
Cannot render console from 73.104.86.133! Allowed networks: 127.0.0.1, ::1,
127.0.0.0/127.255.255.255
Processing by UsersController#show as HTML
Parameters: {"id"=>"5"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]]
Rendering users/show.html.erb within layouts/application
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendered users/show.html.erb within layouts/application (3.5ms)
Completed 200 OK in 86ms (Views: 81.9ms | ActiveRecord: 0.5ms)