Ruby on Rails: User helper method to read attribute -


i'm trying use helper method determine value of attribute several records. here basic function trying working (from view):

      <% if baseline.where(subject_id: sub.subject_id).first.crf_status(crf) == 1 %>          <td bgcolor="#98fb98" >       <% else %> 

my helper function crf_status(crf), , looks this:

application_helper.rb

def crf_status(crf)     case crf     when baseline 'baseline_status'     when followup3week 'follow_up_3_week'     ...     end end 

so working example if crf_status(baseline) return:

      <% if baseline.where(subject_id: sub.subject_id).first.baseline_status == 1 %>          <td bgcolor="#98fb98" >       <% else %> 

right now, error 'undefined method 'crf_status' baseline'. based on i've read, perhaps have reference applicationhelper in each controller? doesn't sound right. please let me know think.

thanks.

edit. forgot make more clear: crf_status(crf) being passed object array [baseline, followup3week...].

the actual line starts -> if crf.where(subject_id:...

if want return method called in context, use .send method.

baseline.where(subject_id: sub.subject_id).first.send(crf_status(crf)) 

whatever returned method executed. great metaprogramming example. want test against class of instance, use .class method on case line. you'll want return symbols not strings though, this:

def crf_status(crf)     case crf     when baseline :baseline_status     when followup3week :follow_up_3_week     else :default     end end 

edit: changed case type comparison


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -