Topic: in_place_edit / model => validation

Using the in_place_edit feature how would I generally go about putting the information to be updated through the validation?

Edit RHTML:

<h3>Editing account</h3>
<% unless @current_account.blank? %>
<p>
    <b>Email:</b>
    <%= in_place_editor_field "account", "email" %>
</p>
<% end %>

Account Model:
class Account < ActiveRecord::Base
   validates_presence_of     :username
   validates_presence_of     :email
   validates_presence_of     :firstname
   validates_presence_of     :lastname
   
   validates_uniqueness_of   :username
   validates_uniqueness_of   :email
   
   validates_format_of :password, :with => /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,15}$/
   validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
   
   validates_length_of :username, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
   validates_length_of :firstname, :maximum=>30
   validates_length_of :lastname, :maximum=>30
   
   attr_accessor :password_confirmation
   validates_confirmation_of :password
   
   def validate
     errors.add_to_base("Missing Password") if hashed_password.blank?
   end
 
   def self.authenticate(username, password)
     account = self.find_by_username(username)
     if account
       expected_password = encrypted_password(password, account.salt)
       if account.hashed_password != expected_password
         account = nil
       end
     end
     account
   end
   
   def password
     @password
   end
   def password=(pwd)
     @password = pwd
     create_new_salt
     self.hashed_password = Account.encrypted_password(self.password, self.salt)
   end
   
   private
   def self.encrypted_password(password, salt)
     string_to_hash = password + "supernova" + salt
     Digest::SHA1.hexdigest(string_to_hash)
   end
   def create_new_salt
     self.salt = self.object_id.to_s + rand.to_s
   end
end

Accounts Controller:
class AccountsController < ApplicationController
  before_filter :login_required, :only => [ :index, :show, :edit, :update, :destroy]
 
  in_place_edit_for :account, :email
 
  def index
 
  end

  def show
    @account = @current_account
  end

  def new
    @account = Account.new
  end

  def create
    @account = Account.new(params[:account])
     if @account.save
      flash.now[:notice] = 'Account was successfully created.'
    else
       render :action => 'new'
    end
  end

  def edit
    @account = @current_account
  end
 
  def update
  end

  def destroy
    @current_account.destroy
  end
 
  def login
    session[:account_id] = nil
    if request.post?
      @current_account = Account.authenticate(params[:username], params[:password])
      unless @current_account.nil?
        unless session[:return_to].blank?
          redirect_to session[:return_to]
          session[:return_to] = nil
          session[:account_id] = @current_account.id
          flash[:notice] = "Login successful"
        else
          redirect_to :controller => 'accounts'
          session[:account_id] = @current_account.id
          flash[:notice] = "Login successful"
        end
      end
      if @current_account.nil?
         flash[:error] = "Login unsuccessful"
      end
    end
  end

  def logout
    session[:account_id] = @current_account = nil
    flash[:error] = "Logout successful"
    redirect_to :controller => 'accounts'
  end
end

Re: in_place_edit / model => validation

?

Re: in_place_edit / model => validation

Generally:

instead of inplace_editor_field, use in_place_editor, pass it a custom url with :url => {} pointing to an action in your controller where you do the update manually.

Re: in_place_edit / model => validation

thank you.. i got it up and running:) thanks again