Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd suggest you think about this in behavioural rather than implementation terms. You mention there are three contact forms, but what is the underlying use that you're putting each one to? You shouldn't be thinking about <em>forms</em> when you're setting up your model.</p> <p>That having been said, you can achieve what you want using <a href="https://github.com/gtd/validation_scopes" rel="nofollow">the validation_scopes gem</a>. Using validation_scopes, you can define sets of validation rules that you can treat independently. In your controllers, you can then check whichever set of validation rules apply to the context (i.e. which form the user has filled in).</p> <p>In your model you can set up validation scopes named for each form (or better, named for the context in a way that has semantic value, but I don't know enough about your app to know what the contexts are), like this:</p> <pre><code>validation_scope :form_one_errors do |vs| validates :name, :body, :presence =&gt; true end validation_scope :form_two_errors do |vs| validates :aaa, :bbb, :ccc, :presence =&gt; true end </code></pre> <p>Since email needs to be validated in both contexts, you can just set it up as a normal validation (as per your code in the question).</p> <p>Then in the controller for, say, form one, you can check the scope to see if there are any errors for that context. Note that you have to check the errors for the validation scope separately for the regular validation errors.</p> <pre><code>if !message.valid? # Do something with message.errors elsif message.has_form_one_errors? # Do something with message.form_one_errors else # All good end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload