views:

59

answers:

1

Hi Everyone,

I currently have a model that the user can upload a thumbnail to the record which is then shown on the show view page. This thumbnail was originally intended so the user could easily find the case when searching through the index view table of records.

It has become apparent that users are only using this function to upload a thumbnail of the year - for example, they have uploaded a red square with 10 in for each case with a reference number of M10-XXX.

So I was wondering if it's possible to add a helper to the kase model to output an image if the kase reference starts with M10. I could then include in the same helper the image for 2011, 2012 and so on.

At the moment the kase show view calls the uploaded thumbnail:

<% if @kase.avatar.exists? then %>
            <%= image_tag @kase.avatar.url %>
        <% else %>
            <img src="../images/document.jpg" alt="Document" />
        <% end %>

What I would like to do, is add a helper such as:

def kase_thumb k 
  if k.jobno.to_date == Date.today then
    <img src="images/10.png" alt="2010" />
  else 
    <img src="images/no-year-found.png" alt="2010" />
  end
end

Obviously the above wouldn't work because the standard jobno would be M-XXX where is replaced with the last two digits of the current year.

Is is possible to have a helper method that extracts the first two numbers, but ignores the first letter?

I did think about doing this with the date created, but people may retrospectively add cases to the application.

Thanks,

Danny

+1  A: 

Given the string of the case number/identifier:

number = the_case_id[1,2].to_i

Hope this helps.

thomasfedb
So something like: `if number = k.jobno[2,3].to_i == Date.today then`?
dannymcc
number, in this case will contain the number `10` or any 2 digit number.
thomasfedb
Could this be done in an 'if' statement in the view?
dannymcc
Yes. `if the_case_id[1,2].to_i == 10` etc...
thomasfedb
Worked a treat - thanks!
dannymcc