2011-12-19 116 views
1

我使用sorcery gem來管理認證。你可以看到下面的代碼。如何在不更改憑據的情況下更新用戶

模型/ user.rb

class User < ActiveRecord::Base 
    authenticates_with_sorcery! 

    attr_accessible :username, :email, :password, :password_confirmation 

    has_many :clients 
    has_many :orders 

    validates_presence_of :email, :username, :on => :create 
    validates_presence_of :password, :on => :create 
    validates_uniqueness_of :email 
    validates_confirmation_of :password, :message => "confirmation doesn't match password " 
    validates_length_of :password, :minimum => 6 
end 

控制器/ users_controller.rb

class UsersController < ApplicationController 
    before_filter :require_login, :except => [:not_authenticated, :new, :create] 
    skip_authorize_resource :only => [:new, :create] 

    layout "users" 

    def new 
     @user = User.new 
    end 

    def create 
     @user = User.new(params[:user]) 
     if @user.save 
     redirect_to clients_url, :notice => "Bienvenu dans la communauté Bakden!!!" 
     else 
     render :new 
     end 
    end 


#show user profile 
    def show 
     @user = User.find(session[:user_id]) 

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

# edit user profile 
    def edit 
    @user = User.find(session[:user_id]) 
    end 

# update user profile 

    def update 
    @user = User.find(session[:user_id]) 

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

_form.html.erb

<%= simple_form_for (@user),:html => {:multipart => true} do |f| %> 

    <div class="inputs"> 
    <%= f.input :adress %> 
    <%= f.input :city %> 
    <%= f.input :country ,:as => :country %> 
    <%= f.input :telephone, :as => :string %> 
    <%= f.file_field :picture %>  

    <div class="actions"> 
    <%= f.button :submit %> | <%= link_to "cancel", profile_path%> 
    </div> 
<% end %> 

如何編輯地址,電話等用戶信息,而不影響作爲憑據的密碼,電子郵件和用戶名。

回答

2

我使用best-in-place寶石來編輯特定的字段。這裏是Ryan Bates here的視頻。

要查看測試示例檢查this

我覺得這是編輯特定字段時不會影響用戶體驗的最快最簡單的方法,而且如果您只是想更新特定字段(如電話和地址),肯定會起作用。希望能幫助到你。

+0

感謝您迴應如此到位diligently.Sure最好能使它。但是我想用一個單獨的形式有全角度探討的問題 – 2011-12-19 17:15:54

2

示例代碼,從設計,這正好在用戶模式:

# Update record attributes when :current_password matches, otherwise returns 
    # error on :current_password. It also automatically rejects :password and 
    # :password_confirmation if they are blank. 
    def update_with_password(params, *options) 
    current_password = params.delete(:current_password) 

    if params[:password].blank? 
     params.delete(:password) 
     params.delete(:password_confirmation) if params[:password_confirmation].blank? 
    end 

    result = if valid_password?(current_password) 
     update_attributes(params, *options) 
    else 
     self.attributes = params 
     self.valid? 
     self.errors.add(:current_password, current_password.blank? ? :blank : :invalid) 
     false 
    end 

    clean_up_passwords 
    result 
    end 

    # Set password and password confirmation to nil 
    def clean_up_passwords 
    self.password = self.password_confirmation = nil 
    end 
+0

我可以看到方向的方向,但我仍然無法正常工作。當我去滑軌控制檯時,地址和電話屬性爲零 – 2011-12-19 17:34:29

+0

我沒有看到你的意思..?地址和電話在params散列?是:current_password給出並在params散列中有效? – clyfe 2011-12-19 17:45:16

+0

不,他們沒有。所以params [:user]是不夠的? – 2011-12-19 18:11:01

相關問題