Showing check box values in edit page - ruby-on-rails

I'm using the following collection_select in the new and edit pages to let users select contacts and associate them to a group:
<%= f.label :contacts %>
<div><span class="ul">
<% current_user.contacts.all.each do |contact| %>
<%= check_box_tag "contacts[]", contact.id %>
<%= f.label contact.name %>
<% end %></div>
</span>
I would like to show the already selected contacts checked in the edit page of the group. Is there another parameter that I can use along with the check_box_tag?

You can just pass in a true/false after the value
<%= f.label :contacts %>
<div>
<span class="ul">
<% current_user.contacts.all.each do |contact| %>
<% checked_logic = some logic for true/false %>
<%= check_box_tag "contacts[]", contact.id, checked_logic %>
<%= f.label contact.name %>
<% end %>
</span>
</div>
<% end %>

Related

How to post array of objects in rails?

I have the following code in my
new.html.erb
<%= form_tag puppies_path do %>
<% #kennel.each do |puppy| %>
<%= fields_for 'puppies[]', puppy do |p| %>
<div class="field">
<%= p.label :name %><br>
<%= p.text_field :name %>
</div>
<div class="field">
<%= p.label :breed %><br>
<%= p.text_field :breed %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
And puppies[] array variable, which is supposed to post array of objects to controller is posting only single object. Please help to post an array to controller. Thanks in advance!
The usual setup for fields_for is something like this.
<% #kennel.each do |kennel| %>
<%= fields_for :puppies, #kennel.puppies do |p| %>
Yes, I have just found an answer...
In new.html.erb file
<%= form_tag puppies_path do %>
<% 2.times do %>
<%= render 'puppies_group_form' %><br>
<% end %>
<%= submit_tag "Submit" %>
<% end %>
In _puppies_group_form
Name <%= text_field_tag "puppies[][name]" %>
Breed <%= text_field_tag "puppies[][breed]" %>

Change checkbox to change in db in rails?

I have a table "User" with "admin" is "true" or "false". I show it with checkbox_feild. How can i change db if i clicked in checkbox? Thanks all.
<% #users.each do |user| %>
<%= form_for user, do |f| %>
<div class="field">
<%= f.check_box :admin%>
</div>
<% end %>
<% end %>
One way is to write a jquery script that submits the form when checkbox is clicked
<% #users.each do |user| %>
<%= form_for user,:html => {class: 'form'} do |f| %>
<div class="field">
<%= f.check_box :admin%>
</div>
<% end %>
<% end %>
Jquery script would be:
$('.form input[type=checkbox]').change(function(){
$(".form").submit();
});

How do I submit one param multiple times on rails?

So I have Users and Interests and they are associated with a has_and_belongs_to_many relation through an interests_users table. What I want to do is to let users select 5 interests when they create their account through checkboxes, but I don't now how to submit the same param 5 times in the same form and I am currently submiting it with checkbox, but Interest is not a boolean. The code right now is like this:
<div class = "field">
<%= f.label :interests %><br>
<% all_interests = Interest.all.map{ |f| [f.name, f.id] } %>
<% all_interests.each do |interest| %>
<p> <%= interest[0] %> <%= f.check_box :interests %> <p>
<% end %>
</div>
In view use the flowing
<% for interest in Interest.all %>
<%= check_box_tag "user[interest_ids][]", interest.id, #user.interests.include?(interest) %>
<%= interest.name %>
<% end %>
In controller accept params as following
params.require(:user).permit( { interest_ids:[] })
You should try this code :
<div class = "field">
<%= f.label :interests %><br>
<% Interest.all.each do |interest| %>
<p> <%= interest.name %> <%= f.check_box :interests, { :multiple => true }, interest.id, nil %> </p>
<% end %>
</div>

Rails and Postgres array columns

I have a model with an array column (box_ids). In my view I would like to have a field for each of the values in the array and three extra empty fields to be able to add new values to the array. How to go about this? I have the following:
<%= f.fields_for "box_ids[]", #shop do |bid| %>
<div class="form-group">
<%= bid.label :box_id %> Box ID: <%= bid.text_field [WHAT HERE] %>
</div>
<% end %>
I don't know if this is the right approach but in any case I have no method to supply to text_field.
Any suggestions?
Edit:
This works:
<% #shop.box_ids.each do |bid| %>
<div class="form-group">
<%= label_tag :box_id %> Box ID: <%= text_field_tag "box_ids[]", bid %>
</div>
<% end %>
<% 3.times do %>
<div class="form-group">
<%= label_tag :box_id %> Box ID: <%= text_field_tag "box_ids[]" %>
</div>
<% end %>
But that requires special handling in the controller - I would like to avoid that if possible.
<%= f.fields_for "box_ids[]", #shop do |bid| %>
<div class="form-group">
<%= bid.label :box_id %> Box ID: <%= bid.text_field :box_ids %>
</div>
<% end %>

Radio button data not saved

I'm playing around with Form Helpers. I found some code from another SO question and thought it was pretty efficient at creating radio buttons with an elegant loop. Now that I've incorporated it, it doesn't save the data (e.g. the category value is not being saved to the project table)
Please see code below of _form.html.erb
<%= form_for(#project) do |f| %>
<% if #project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% #project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="form_row">
<label for="category">Category:</label>
<% [ 'checklist', 'process'].each do |category| %>
<br><%= radio_button_tag 'category', category, #category == category %>
<%= category.humanize %>
<% end %>
<br>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Your radio button parameter is being created outside of the project structure. If you look at params, you'll probably see
{:category => "your_category", :project => {...project params...}}
It is because you're using the radio_button_tag instead of the regular form helper. Try this instead:
f.radio_button :category, category, :checked => (#category == category)
Also, as Justin said, make sure :category is included in the project_params in your controller.

Resources