Witam, robię małą aplikację, która ma za zadanie sprawdzić poprawność numeru karty kredytowej.
Oto link do zadania :
Tutaj mam trzy modele
card_type.rb
# Method used to get the card type.
module Card_type
def find_type(credit_card)
# Making sure that the card number is passed as a string
credit_card = credit_card.to_s
# Set of statements to return the appropriate card type
# Firstly the code checks the cards's length
# Secondly regexp, returns 0 which signals validation
return "AMEX" if credit_card.length == 15 && (credit_card =~ /^(34|37)/) == 0
return "Discover" if credit_card.length == 16 && (credit_card =~ /^6011/) == 0
return "MasterCard" if credit_card.length == 16 && (credit_card =~ /^(5[1-5])/) == 0
return "VISA" if [13,16].include?(credit_card.length) && (credit_card =~ /^4/) == 0
return "Unknown"
end
end
luhn_validation.rb
# Method/ module used to check if card is valid/invalid
module Luhn_validation
def luhn(cc_number)
result = 0
nums = cc_number.to_s.split("")
nums.each_with_index do |item, index|
if index.even?
result += item.to_i * 2 >9 ? item.to_i*2-9 : item.to_i*2
else
result +=item.to_i
end
end
if (result % 10) == 0
return true
else
return false
end
end
end
#Tests
#puts luhn(4111111111111111)
#puts luhn(4111111111111)
#puts luhn(4012888888881881)
#puts luhn(378282246310005)
#puts luhn(6011111111111117)
#puts luhn(5105105105105100)
#puts luhn(5105105105105106)
#puts luhn(9111111111111111)
credit_card.rb
require File.dirname(__FILE__)+"/card_type.rb"
require File.dirname(__FILE__)+"/luhn_validation.rb"
class CreditCard < ActiveRecord::Base
include Card_type
include Luhn_validation
# apply methods on the inputed number
def self.initialize(cc_number)
self.type = find_type(cc_number)
self.validity = luhn_validation(cc_number) ? "valid" : "invalid"
end
end
Mój problem polega na zrobieniu prostego view, do moich modeli.
Chciałbym zrobić prosty formularz label, field,button
a w indexie żeby wyświetlało wszystkie wprowadzone dane np:
numer wprowadzonej karty//typ karty// luhn działa czy nie
Próbowałem już stworzyć to co chcę za pomocą prostego scaffolda, ale nie potrafiłem accessować method z modelu w controllerze. Railsy wyświetlały komunikat na temat nie rozpoznanej metody “find_type()”
Kolejne pytanie, czy to dobra praktyka że zawarłem moje metody w modelu?
Jeżeli znajdują tutaj się błędy w kodzie czy jakieś “nie dobre nawyki”, też proszę o zwrócenie uwagi.
Proszę o wyrozumiałość, mam doświadczenie małe doświadczenie z innych języków programowania,
nie miałem też większych problemów z napisaniem kodu: , ale kiedy dochodzi do przełożenia tego na view kompletnie padam.
Dziękuje z góry za wasze odpowiedzi
Adrian