kdr
kdr

Reputation: 217

validation error in rails

I am using sexy validation in rails to validate my fields. How ever the validation works fine while using a new action but not working fine when updating the same using update action. While updating i am getting template missing error. The controller

class Admin::AccruedFundsController < ApplicationController

  layout 'admin-layout'
  before_action :authenticate_user!
  #load_and_authorize_resource

  def funds_params
    params.require(:accrued_fund).permit(:id, :society_id, :satutory_reserve_fund, :building_fund, :depreciation_fund, :all_other_fund)
  end

  def index
     respond_to do |format|
    format.html
    format.json { render json: AccruedFundsDatatable.new(view_context,current_user) }
    end
  end

  def show
    @funds = AccruedFund.find(params[:id])
  end

  def new
    @funds = AccruedFund.new
  end

  def create

    @funds = AccruedFund.new(funds_params)
    if @funds.save
      flash[:success] = "Successfully Created!"
      redirect_to :action => 'index'
    else
      flash[:error] = "Sorry! Could not complete the request, please try again!"
    render :action => "new" 
    end 
  end 

  def edit
    @funds = AccruedFund.find(params[:id])
  end

  def update

    @funds = AccruedFund.find(params[:id])
      if @funds.update_attributes(funds_params)
        flash[:success] = "Successfully Updated!"
         redirect_to :action => 'index'
      else
        flash[:error] = "Sorry! Could not complete the request, please try again!"
         render :action => 'update'
      end
  end   

  def destroy
      if AccruedFund.find(params[:id]).destroy
      redirect_to :action => 'index'
      flash[:success] = "Successfully Deleted!"
    else
      flash[:error] = "Sorry! Could not complete the request, please try again!"
      redirect_to :action => 'index'
   end  
 end
end

The sexy validation used in model is as follows:

class AccruedFund < ActiveRecord::Base

  belongs_to :society

  validates :society, :presence => true,
                      :associated => true

  validates :satutory_reserve_fund, :presence => true,
                                    :numericality =>true 

  validates :building_fund, :presence => true,
                            :numericality => true 

  validates :depreciation_fund, :presence => true,
                                :numericality => true

  validates :all_other_fund, :presence => true,
                             :numericality => true

end

Upvotes: 0

Views: 108

Answers (1)

user1476061
user1476061

Reputation:

There is no template for update action render :action => 'update'.

You should render edit action.

Note:
- render :new would work also fine.
- when you use render methond, you have to use flash.now[:error](or notice, or success). Otherwise, flash will be triggered on next page.

Upvotes: 1

Related Questions