

The use of Rails trains wizards on non-existent methods
source link: https://www.codesd.com/item/the-use-of-rails-trains-wizards-on-non-existent-methods.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The use of Rails trains wizards on non-existent methods
Normally, when using form helpers in Rails, each field directly correlates to a method on the appropriate object.
However, I have a form (user signup) that needs to include fields that are not part of the user model itself (for instance, card details) but need to appear.
How can I build the form so that I can get the necessary fields, and validate them as I need to (so that it fits with all my other validations) without dirty-ing my user model?
I'm not sure what you mean by "dirty-ing" your user model, but you can do this using the attr_accessor method. This will allow you to create an attribute for the model that can be used for validations:
class Widget < ActiveRecord::Base
attr_accessor :confirmation_email
validates_length_of :confirmation_email, :within => 1..10
end
To only run the validation at certain times, you could use an :if
condition:
validates_length_of :confirmation_email, :within => 1..10, :if => Proc.new { |widget| widget.creation_step > 2 }
You can also use a class method, like: :if => :payment_complete
. With these you should be able to achieve the functionality you want. As far as I know there isn't any more concise way.
Recommend
-
95
Harry Potter: Wizards Unite - Pokemon Go 类型的哈利波特手游,目前只在新西兰和澳大利亚上架 - NEXT
-
27
About this Episode In today’s episode we have one of our favorite recurring guests, Brooklyn Zelenka, joining us once again! Brooklyn has been on the show in both the first and second seasons to s...
-
5
Washington Wizards menuju Dallas untuk menghadapi Mavericks, Sabtu jam 9 malam EST. The Wizards telah menjadi salah satu tim terpanas di NBA, tepat dalam campuran balapan playoff Wilayah Timur. Dallas hanya berada di ambang play-in t...
-
3
6006 members Technology The latest news, reviews and features from the digital and analog world.
-
5
YouTube caters to your non-existent attention span by pointing out exactly which parts of a video are worth watching By Will Sattelberg...
-
7
Comments mehula@mehul-machine:~/mirrord$ target/debug/mirrord extract blah thread 'main' panicked at 'called `Re...
-
6
Building $10m ARR business in so called “non existent” markets: Introducing UnPluggd speaker: Bizom’s Lalit Bhise ...
-
3
EnergyThe Dumbest Reactions to the Non-Existent Gas Stove BanWelcome to 2023, where the culture wars have come for your coo...
-
5
Non-Existent Vaccine Microchips Could Soon Be Banned in Missouri
-
9
Non-existent users show up in SignInActivity data (or how logs continue to disappoint) One of the common questions I get nowadays is “give me a list of all users that haven’t logged in in the past...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK