2012-11-13 86 views
0

我有一個叫做「backend」的名字空間,它受Devise保護。 我現在想讓用戶編輯他們的profil。如何允許設計用戶編輯他們的profil?

所以我在後端創建了一個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"=>"[email protected]"}, "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 

但它失敗...

回答

0

如果沒有反應,那通常會顯示一個驗證錯誤,在您的情況下可能是密碼丟失或其他數據不屬於您的表單,但仍在驗證中。要進行調試,請將@user.errors.inspect輸出到您的日誌文件或在您的視圖中查看error_messages_on @user

一般來說,看看如何允許用戶編輯自己的(部分)簡介這些指南:

+0

感謝關心我的問題。我將用de日誌和更多信息編輯我的帖子。 –

+0

請分享錯誤訊息,它應該在'@ user.errors'中。 – Thilo

相關問題