custom validation in rails - select 2 from 4 - ruby-on-rails

I know from looking at the Rails api that there's a way to create custom validations, but I can't figure it how to do it with this. I want the user to only be able to select 2 of the 4 check_boxes... Pretend the controller is named favorites_controller and the favorites table has these attributes
Is there a way to make them only select 2 of the 4?
<div class="field">
<%= f.label(:sex, "Sex") %>
<%= f.check_box(:sex) %> </br>
</div>
<div class="field">
<%= f.label(:chocolate, "Chocolate") %>
<%= f.check_box(:chocolate) %> </br>
</div>
<div class="field">
<%= f.label(:computers, "Computers") %>
<%= f.check_box(:computers) %> </br>
</div>
<div class="field">
<%= f.label(:comicbooks, "Comic Books") %>
<%= f.check_box(:comicbooks) %> </br>
</div>
#obrok how far off am I with this function, using a before_save, I know it's not right but can you steer me in the right direction. How do I trigger the validation function on the form submission? I'm a newbie (and generally not very smart :(
before_save :check_box_count
....
def check_box_count
#check_box_count = check_box(:sex) + check_box(:chocolate) + check_box(:computers) + check_box(:comicbooks)
unless #check_box_count == 2 return false
end

You can do this simply by writing a method which counts how many checkboxes are ticked and then validating that.
Edit:
Something like this I think.
validates_inclusion_of :check_box_count, :in => 2..2, :message => "Two checkboxes should be ticked"
...
private
def check_box_count
[sex, chocolate, computer, comicbooks].select{|x| x }.count
end

Related

Serialized fields_for not populating from db on edit

On the edit page for this form all of the fields outside of the fields_for tag (inbox name, automatic reconciliation, and a few others not listed here) are all populating based on their corresponding db value. However, everything inside the fields_for tag are not, even though they're posting to the db just fine.
I posted :group_member_roles as an example but there are a few other fields inside their own other fields_for that are doing the same thing. It's just confusing that it will post to the db but not display on edit.
The more I read into fields_for the more I feel like I'm not using it correctly. It seems to be more inclined to populating db tables outside of the one your form is currently referencing, but I'm just trying to serialize data within the inbox table. When I look at the :group_member_roles column I want it to be an array/hash containing process true/false, action add/delete, and a string of values.
#_form.html.erb
<%= form_for(#inbox) do |f| %>
<%= f.label :inbox_name %>
<%= f.text_field :name, placeholder: "Inbox Name" %>
<%= f.label :automatic_reconciliation, "Turn on/off automatic reconciliation" %>
<div class="switch small">
<%= f.check_box :automatic_reconciliation, class: "switch-input" %>
<label class="switch-paddle" for="inbox_automatic_reconciliation">
<span class="show-for-sr">Automatic reconciliation</span>
<span class="switch-active" aria-hidden="true">On</span>
<span class="switch-inactive" aria-hidden="true">Off</span>
</label>
</div>
<%= f.fields_for :group_member_roles do |group_member_roles| %>
<h4>Group Member Roles</h4>
<%= group_member_roles.label :process, "Turn On/Off Processing" %>
<div class="switch small">
<%= group_member_roles.check_box :process, class: "switch-input" %>
<label class="switch-paddle" for="inbox_group_member_roles_process">
<span class="show-for-sr">Group Member Roles Processing</span>
<span class="switch-active" aria-hidden="true">On</span>
<span class="switch-inactive" aria-hidden="true">Off</span>
</label>
</div>
<%= group_member_roles.label :action, class: "hide" %>
<%= group_member_roles.select :action, ["Add", "Delete"], { selected: "Add" }, { class: "hide" } %>
<%= group_member_roles.label :values %>
<%= group_member_roles.text_field :values, placeholder: "1234, 1337, 1986" %>
<% end %>
Thanks in advance for any help or guidance.
The fields were being stored as a hash and the field was looking for an object to populate with so I added an OpenStruct dummy object to the fields_for to make it so. If anyone can think of a better way please let me know as this is pretty ugly code.
<%= f.fields_for :group_member_roles, OpenStruct.new(f.object.group_member_roles) do |group_member_roles| %>

Best way to dropdown a list of country in rails 4

i have a form where users input some information and I want to add to this form a country and I am wandering what is the best way to do this (seed data, Csv, ...) i don't want to use a gem
Since this is essentially static data, I wouldn't even store it into the database. I would create a PORO that had a class method that returns a 2 dimensional array (country with country abbreviation). It's just a matter of sourcing that data.
Here's a list you can use.
https://github.com/karmi/localized_country_select/blob/master/locale/en.rb
I use the country_select gem. It is very simple to use. Just supply model and attribute as parameters:
country_select("user", "country")
Here is an example from one of my forms:
<%= f.fields_for (:address) do |b2| %>
<%= b2.input :street %>
<%= b2.input :city, :label => "City/Suburb" %>
<%= b2.input :state %>
<%= b2.input :post_code %>
<div class="control-group">
<label class="control-label">Country</label>
<div class="controls">
<%= b2.country_select :country, ["Australia", "New Zealand"] %>
</div>
</div>
<% end %>

Ruby on rails drop down box Beginner lvl

So I am a beginner in ROR, like I know it for a month (school assignment and we don't get a cursus we need to use 'google')
So I want a dropdown box with a list of all my cities. Then if I pick a city I need to save the city_id in my database together with the date. The code I have so far seem to work except when I click on save it says that city is empty (and it can't be empty because of the failsave)
this is my code
<div class="field">
<%= f.label :stad_id %><br />
<% cities_array = Stad.all.map { |stad| [stad.naam, stad.land] } %>
<%= select_tag(Stad, options_for_select(cities_array)) %>
</div>
<div class="field">
<%= f.label :datum %><br />
<% datum = Time.now.to_s(:db) %>
<%= f.text_field :datum, :value => datum.inspect, :readonly => true %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I don't really know what I am doing wrong except I have an eery feeling I don't actually give the command to save it.
help is much thanked
sincerely
Robin
There are a few things I notice here that are wrong.
1) Put the creation of the cities_array into your controller, not in your view:
def edit
#something = Something.find(params[:id])
#cities_array = ... whatever ...
end
2) When creating your cities_array, you need to specify the ID of the city as the second parameter, like this:
#cities_array = Stad.all.map { |stad| [stad.naam, stad.id] }
3) The select_tag call isn't for Stad, it's for the model you're trying to save. For example, your form might look like this:
<%= form_for #something %>
<%= f.label :city %>
<%= f.select :city_id, #cities_array %>
# or!
<%= select :something, :city_id, #cities_array %>
<% end %>
I hoep this clears things up for you.

How to define a select field in a nested form?

What is the correct way to include a select field within a nested form in Rails 3.2?
I currently have
//IN THE PARENT FORM
<%= f.fields_for :crayons do |crayon| %>
<%= render 'caryon_fields', :f=>crayon %>
<% end %>
//IN THE PARTIAL
<div class="nested-fields">
<%= select (:crayon, :color, [['Red',1],['Blue',2],['Yellow',3],['Green',4]] ) %>
</div>
This is not saving the selected value to the database. I assume this is because the builder is not being passed.
How should I define a select field with hardcoded options in a nested field? Do I need to change tag, i.e. select_tag or collection_select. I'm still unsure of the differences between all these. If anyone can point me towards a clear description it would be much appreciated.
Thanks!
You need associate to your form in your partial like that :
<div class="nested-fields">
<%= f.select (:crayon, :color, [['Red',1],['Blue',2],['Yellow',3],['Green',4]] ) %>
</div>
Whitout partial it's :
<%= f.fields_for :crayons do |crayon| %>
<div class="nested-fields">
<%= crayon.select (:crayon, :color, [['Red',1],['Blue',2],['Yellow',3],['Green',4]] ) %>
</div>
<% end %>

How should I handle this Rails use case?

I have an Exercise model, which has these columns (pseudo Ruby):
model Exercise do
string :name
calories_burned :float
end
I want that when a user adds an exercise to be able to do it in this fashion:
if previous exercises exist
show a select element with names of existing added
show a checkbox to allow adding a new one, switching the input
field to a textfield
else
show a textfield
The thing is, I don't know how I should put this in my view. Here's how the else case is handled:
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
I have something like this now:
<div class="field">
<% if #exercise_added %>
<div id="select_div">
<%= select_tag :name,options_for_select(#exercise_added) %>
<input type="checkbox" name="custom_type_checked" id="which_type">New type?</input>
</div>
<% end %>
<div id="regular_field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
</div>
In #exercise_added I have a list of names of all exercises from the database. What would be the best/cleanest way of handling this?
EDIT: For now,I have a textfield and a select, and by using Javascript, I'm changing the name of the element ( and hiding the other one ). So far, it's working, but I'd still be interested if other approaches exist.
You can check if the array #exercise_added is empty or not and show the select field or text field accordingly.
<div class="field">
<% if !#exercise_added.empty? %>
<div id="select_div">
<%= select_tag :name,options_for_select(#exercise_added) %>
<input type="checkbox" name="custom_type_checked" id="which_type">New type?</input>
</div>
<% else %>
<div id="regular_field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<%end%>
</div>
I would, by default, have the select box and a button shown, with the textbox hidden unless a variable #show_textbox is true. Something like this:
<div class="field">
<div id="select_div">
<%= select_tag :name,options_for_select(#exercise_added) %>
<%= f.submit "New Exercise" %>
</div>
<div id="regular_field" <%= hidden_unless #show_textbox %> >
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
</div>
Where I've written a helper function
def hidden_unless cond
raw "style=\"display: none;\"" unless cond
end
# this one is helpful, too
def hidden_if cond
raw "style=\"display: none;\"" if cond
end
Then, in my controller, check if the "New Exercise" button was pressed. If so, essentially set #show_textbox to true and then re-render the new form.
def create
# .....
# did we get here because the user pressed "New Exercise"?
if params[:commit].eql?("New Exercise")
#show_textbox = true
# probably some other code to replicate what happens in your #new action
render :action => new
end
# ....
end
You can check in your controller if the :name field has any text in it, and use that to override the select box.
This should work without javascript. I'd add some jQuery to replace the button with either a link or a check box, with the click handler for that connected to a function that shows the textbox, i.e. $('#regular_field').toggle();.
I didn't deal with hiding the select box. I actually think it might be better to leave that available. You could hide it using a similar method, anyways.
What if you used two forms? One form to handle the case when the exercise is in #exercise_added, and a second form to handle the creation of a new exercise. It might even boil down to the difference between a POST and a PUT, depending on what you're doing once an exercise is submitted from the drop-down list.
I'd be curious to see more of the code, as well as the controller, since it seems like this form might be nested?

Resources