2011-12-19 7 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 가장 어쨌든 다 했지을 할 수 있도록 응답을 위해 내가 전체 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이됩니다 –

+0

무슨 뜻인지 모르겠군요 ..? 주소와 전화 번호가 params 해시에 있습니까? Is : current_password가 주어졌고 params 해시에서 유효합니까? – clyfe

+0

아니요. 그래서 params [: user]는 충분하지 않습니까? –

관련 문제