如何允许设备用户编辑他们的个人资料?

时间:2012-11-13 10:53:20

标签: ruby-on-rails

我有一个名为“backend”的名称空间,受到Devise的保护。 我现在想让用户编辑他们的个人资料。

所以我在Backend中创建了一个users_controller。 这是users_controller代码:

class Backend::UsersController < ApplicationController
layout 'admin'
before_filter :authenticate_user!

    def index
        @users = Backend::User.all

         respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
    end

    def show
    @user = Backend::User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @user }
    end
  end

    def edit
        @user = Backend::User.find(params[:id])
    end

    def update
    @user = Backend::User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'Article was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
end

当我继续backend_users_path时,会列出所有用户。我想允许只编辑自己的个人资料。 所以我进入编辑页面:<%= link_to "Edit", edit_backend_user_path(backend_user.id) %>。 这是编辑页面代码:

<%= simple_form_for @user do |f| %>
  <div><%= f.label :email %><br />
  <%= f.input :email, :autofocus => true %></div>

  <div><%= f.submit "Update" %></div>
<% end %>

还有我的问题:当我尝试修改电子邮件地址时,没有任何反应。更新失败。

我该怎么做? 我很失落。 先谢谢。

这是日志文件:

Started PUT "/backend/users/1" for 127.0.0.1 at 2012-11-13 12:13:51 +0100
Processing by Backend::UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"wWrUDh7LVWhP+P7OWO6laDWaCKInxk37AA2BPuQWAI4=", "backend_user"=>{"email"=>"grellazzi@laposte.net"}, "commit"=>"Update", "id"=>"1"}
  [1m[35mBackend::User Load (0.0ms)[0m  SELECT `backend_users`.* FROM `backend_users` WHERE `backend_users`.`id` = 1 LIMIT 1
  [1m[36mBackend::User Load (0.0ms)[0m  [1mSELECT `backend_users`.* FROM `backend_users` WHERE `backend_users`.`id` = ? LIMIT 1[0m  [["id", "1"]]
  [1m[35mSQL (1.0ms)[0m  BEGIN
  [1m[36m (0.0ms)[0m  [1mCOMMIT[0m
Redirected to http://localhost:3000/backend/users/1
Completed 302 Found in 23ms (ActiveRecord: 1.0ms)


Started GET "/backend/users/1" for 127.0.0.1 at 2012-11-13 12:13:51 +0100
Processing by Backend::UsersController#show as HTML
  Parameters: {"id"=>"1"}
  [1m[35mBackend::User Load (0.0ms)[0m  SELECT `backend_users`.* FROM `backend_users` WHERE `backend_users`.`id` = 1 LIMIT 1
  [1m[36mBackend::User Load (0.0ms)[0m  [1mSELECT `backend_users`.* FROM `backend_users` WHERE `backend_users`.`id` = ? LIMIT 1[0m  [["id", "1"]]
  Rendered backend/users/show.html.erb within layouts/admin (0.0ms)
Completed 200 OK in 7ms (Views: 5.0ms | ActiveRecord: 0.0ms)

感谢您的链接,我尝试使用

修改我的user_controller
if params[:user][:password].blank?
      params[:user].delete("password")
      params[:user].delete("password_confirmation")
    end

    @user = User.find(current_user.id)
    if @user.update_attributes(params[:user])
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to root_path
    else
      render "edit"
    end

但它失败了......

1 个答案:

答案 0 :(得分:0)

如果没有任何反应,通常表示验证错误,在您的情况下可能是缺少密码或其他不属于您的表单但仍在验证的数据。要对此进行调试,请将@user.errors.inspect输出到日志文件或查看视图中的error_messages_on @user

一般情况下,请查看这些指南,了解如何允许用户编辑其(部分)个人资料: