0

我有一个名为“后端”的命名空间,它受设计保护。我现在想允许用户编辑他们的个人资料。

所以我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

但它失败了......

4

1 回答 1

0

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

一般来说,请查看这些指南,了解如何允许用户编辑他们的(部分)个人资料:

于 2012-11-13T10:58:50.537 回答