2011-12-19 18 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 gemを使用して特定のフィールドを編集します。 Ryan Bates hereの動画です。

テスト例の確認はthisです。

これは、ユーザーエクスペリエンスを損なうことなく特定のフィールドを編集する最も簡単で簡単な方法であり、電話や住所などの特定のフィールドを更新する場合には間違いなく機能します。それが役に立てば幸い。

+0

感謝の代わりにdiligently.Sure最高のit.Butを作ることができるので、応答するために、私は全体のperpectiveを持っている別のフォームを使用したいです –

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

私は方向が方向を見ることができますが、私はまだ動作しません。レールコンソールに行くと、アドレスと電話の属性はゼロになります –

+0

私はあなたが何を意味するか見ていない..?アドレスと電話はparamsハッシュで表示されますか? is:current_passwordは指定され、paramsハッシュで有効ですか? – clyfe

+0

いいえ、そうではありません。だから、params [:user]は十分ではありませんか? –

関連する問題