Ruby on Rails - Validations and Before Filter method to substitute User Inputted Values -


In my ruby ​​on the railway app, I have a sign-up form where users have to enter some data. I have strict verification that allow only those values ​​which are members of an array. This is not part of my app, but it uses the same concept that I want to apply.

Say I wanted an area where the user had entered a superhero name. There was such an array in my beliefs.

  Suprheros = [ 'Batman', 'Superman', 'Captain America', 'Wonder Woman', 'Spiderman'] Manytaon_vivrn_: superheroes ,: a = & gt; Superheroes  

If a Clark Clark has entered, for example, verification will fail, I have created a new array.

  ALIASES = [ 'Bruce Wayne' Clark Kent ',' Steven Rogers 'Rajkumari Dayna', 'Peter Parker']  

I It seems that before submitting the form (updated verb), aliases are converted into a superhero array for values ​​in the array.

I was thinking that such a thing could work.

DIF alias_to_superhero ALIASES.each_do | Alias ​​| I = 0 while i & lt; SUPERHEROES.length alias.gsub (nickname, "# {Suprheros [i}}") i ++ end end end

And then finally I am on top of my beliefs row Maybe

  before_update: alias_to_superhero  

Any suggestions?

You name it can validate that hero or superhero superhero The name is but before you save it, you change the aliens with heroes' matching heroes. Verification of the hero gains aka place with the name after that you keep them input untouched until all validation successful

  validates_inclusion_of: superhero. In = & gt; Superhero + ALIAS before_save: private def replace_alias_with_hero_name replace_alias_with_hero_name if ALIAS.include (superhero) self.superhero = superheroes [ALIAS.find_index (superhero)] end end  

This solution only when both arrays Work? The same size and hero names and nicknames are in the same place in the array. A more flexible version will probably work on a hash in this way:

  HEROS => {'Superman' = & gt; ['Clark Kent', 'c. Kent '...] ...}  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -