byCoder
byCoder

Reputation: 9184

Check db value in rails view

Is it good to check db value in view, not in controller? Now i have such view:

- @articles.each do |art|
  -if art.QUANTITYM > 0
    =art.PRICEM
     %br
       = art.PRICEM * @currency.currencyvalue

On my previous rails 3.0.9 project (now 3.2.6), all was good, but now it gives me:

undefined method `>' for nil:NilClass

Also my method in controller look's like this:

def category
    @type_details = Type.find_by_TYP_ID(params[:type])
    @search_trees = SearchTree.find(:all, :include => [:designation], :conditions => { :STR_ID_PARENT => params[:cat]})


    @strlookup = StrLookup.find(:all, :conditions => { :STL_STR_ID => params[:cat]})
    @genart = GenericArticle.all(:conditions => { :GA_ID => @strlookup.map(&:STL_GA_ID)})

    @type = params[:type]
    @la_typs = LinkLaTyp.find(:all, :conditions => { :LAT_TYP_ID => params[:type], :LAT_GA_ID => @genart.map(&:GA_ID)})    


    data = "{ label : 1,  children : [{label : 1},{label : 1}]  }"
    #puts JSON.pretty_generate(data)


    if @genart.blank?
      @articles = nil

    else
      @linkla = LinkArt.find(:all, :conditions => { :LA_ID => @la_typs.map(&:LAT_LA_ID), :LA_GA_ID => @genart.map(&:GA_ID)})    
      @pre_articles = Article.find(:all, :include => [:supplier], :conditions => {:ART_ID => @linkla.map(&:LA_ART_ID)}, :order => "SUPPLIERS.SUP_BRAND, ARTICLES.QUANTITYM asc")
      @articles = Kaminari.paginate_array(@pre_articles).page(params[:page]).per(20)
    end


    @currency = Currency.find(:first)

    #@cart = current_cart #NOTE THIS!!!
    respond_to do |format|

      format.html # index.html.erb
      format.xml  { render :xml => @search_trees }
      #format.json { render :json => @search_trees }
    end
  end

How to check my db Quantity and in view accoding to value display price? Or it is "criminal", and i must do this in controller? Than how to do this check in controller and display it in view?

Note, this is non-my db, so it's very huge, and have a lot of tables, and data in them.

Upvotes: 1

Views: 363

Answers (1)

Mischa
Mischa

Reputation: 43298

No problem checking it in the view. You get the error because art.QUANTITYM is nil. To prevent this error you have to check if it's nil first and then if it's bigger than zero:

- if art.QUANTITYM.present? && art.QUANTITYM > 0

Upvotes: 1

Related Questions