简体   繁体   中英

Having fields be conditionally required in rails model?

I am working on a rails app for a moving company. I am working on a way for leads to enter data that will be submitted. There is a lot of very straight forward information (name, email, phone, etc.) but they also need to enter an origin Address, Zip code, city, state and country and same for a destination. I want the State box to be a select box with a list of states/provinces, but ONLY if the country is the United States or Canada. If the country is not the United States or Canada, the box will disappear/they will not be able to submit a value. Is this possible in rails, or is this more likely something I would use jquery for? Would it be implemented in the model, controller or view (it seems like the model for the validation, and the view for the presentation of the box, but I'm not sure)?

I'm still kinda new to rails, so I'm sorry if this should be a simple answer. I'm using rails 4 for this app.

You seem to be on the right track! The hiding/showing of the states/provinces box would be handled by jQuery. You could also use an ActiveRecord validation in your model to ensure that the state field is blank if the country isn't USA or Canada:

class MyClass < ActiveRecord::Base
  validates :state, presence: false, unless: Proc.new { |m| m.country.in?["USA", "Canada"] }
end

conversely:

validates :state, presence: true, if: Proc.new { |m| m.country.in?["USA", "Canada"] }

You can learn more about custom validations at the RailsGuides page for Active Record Validations . Section 5 talks about conditional validations using custom methods and procs. Section 6 talks about even more powerful custom validations but I doubt you need to go that far here.

To add to sixty4bit 's answer (which I upvoted), you should put your validations into Rails' backend, only handling the response with JS.


Convention over configuration calls for you to use as much of the inbuilt framework functionality as you can; when you start configuring validations etc through JQuery, you end up with code all over the place, often leading to massive bloat (repetition of code etc).

Rails' validation system is very good -- you can send any set of params through your controller and the model will accept/reject depending on whether the data fits the validations.

Thus, I would definitely suggest putting a validator in your model; I'd do it slightly differently than sixty4bit 's answer ( you can use strings with if / unless ):

#app/models/model.rb
class Model < ActiveRecord::Base
   validates :state, inclusion: { in: %w(usa canada),
    message: "%{value} is not a valid state" }, unless: "state.blank?"
end

--

it seems like the model for the validation, and the view for the presentation of the box

In addition to the model validation (which is for the params alone), you'd need some sort of front-end mechanism to deal with the blanking-out of the select box.

This can be done relatively simply:

#app/assets/javascripts/application.js
$(document).on("change", "select#state", function(e) {
    if ($(this).val() != "USA") || ($(this).val() != "Canada") {
       $(this).attr("disabled", true);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM