Saving check box values to the database - ruby-on-rails

I allow users to group contacts together using checkboxes in the new and edit forms:
<% current_user.contacts.all.each do |contact| %>
<% checked = #group.contacts.include?(contact) %>
<%= check_box_tag "contacts[]", contact.id, checked %>
<%= f.label contact.name %>
<% end %>
Until recently, I was able to use the following code in create action to create a group:
def create
#group = current_user.groups.new(group_params)
#contacts = Contact.find(params[:contacts])
#group.contacts << #contacts
if #group.save
......
end
To ensure that already checked values aren't added to a group during the update action, I used a hidden tag in the form.
<%= hidden_field_tag "contacts[]",'' %>
<% current_user.contacts.all.each do |contact| %>
<% checked = #group.contacts.include?(contact) %>
<%= check_box_tag "contacts[]", contact.id, checked %>
<%= f.label contact.name %>
<% end %>
Now, I'm unable to create or update a group as the hidden_field value is also passed along. An Active Record not found : Couldn't find all Contacts with 'id': (, 3, 4) (found 2 results, but was looking for 3) error. How can I solve this?

I think you want the hidden field tag to be nil rather than an empty string
<%= hidden_field_tag "contacts[]", nil %>
But as you're using rails 4 then collection_check_boxes might be of interest

Related

Using a loop to create 10 similar fields in Rails

I'm a beginner on my first project. If I have 10 similar fields, is there a way to create them dry so they would still work with validations? I would also like to put the loop number in the attribute name, because the attributes are named field_name_1 through to field_name_10, for example.
<% 10.times do |asdf| %>
<%= f.input :field_name_(asdf + 1), etc %>
<% end %>
This may work !!!
<% (1..10).each do |n| %>
<%= f.input ("field_name_"+n.to_s).to_sym, etc %>
<% end %>

form to conditionally display fields based on array position

the following rails controller action, creates an array of records to be created:
#guests = []
#quantity.times do
#guests << Guest.new
end
Then the form invokes the array of records to be created in the following manner
<%= form_tag guests_path do %>
<% #guests.each do |guest| %>
<%= fields_for 'guests[]', guest do |f| %>
The goal is to render some fields only for the first of these records/
How can the index value of the first guest be invoked (various attempts such as if #guests[0] generate errors.
I think what you are looking for is each with index
<%= form_tag guests_path do %>
<% #guests.each_with_index do |guest,index| %>
# Do something with index
<%= fields_for 'guests[]', guest do |f| %>

Rails: Find the ID of a record after using exists?

Using two different models, RecordLabel and Artist, I want to link to their pages if the record is found using their usernames. I have no problems finding if the record exists, but I can't figure out how to find the ID of that record. What I have:
<% if RecordLabel.exists?(:username => "#{#artist.artist_profile.record_label_name}") %>
<%= link_to #artist.artist_profile.record_label_name, record_label_path(RecordLabel.find(### NEED RECORD LABEL ID ###) %>
<% else %>
<%= #artist.artist_profile.record_label_name %>
<% end %>
You can get the record very easily this way (if it exists):
RecordLabel.where(:username => "#{#artist.artist_profile.record_label_name}").first
So, your code becomes:
<% if RecordLabel.exists?(:username => "#{#artist.artist_profile.record_label_name}") %>
<%= link_to #artist.artist_profile.record_label_name,
record_label_path(RecordLabel.where(:username => "#{#artist.artist_profile.record_label_name}").first) %>
<% else %>
<%= #artist.artist_profile.record_label_name %>
<% end %>
This should work and solve your problem.

Ruby on Rails: How to print contents of variable in view and make checkbox?

I am trying to create a checklist in rails using form_for. This checklist is taken from a table which I gained in the create action of my sign_ins controller:
#content = OrientationContent.where(site_id: session[:site_id])
In my view I want to use the form_for helper to iterate through the list in #content:
<%= form_for(:sign_ups ) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
However this is not working and it produces two square brackets on the page: [].
How do I go through the list and print the name while creating a check box on the left of it? The check box does not have any meaning or data, I just need it present for reference.
Solved:
In the controller, need to pluck an individual field:
#content = OrientationContent.where(site_id: 1).pluck(:content)
In the view, structure as so:
<%= form_for(:sign_ups) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>

Rails: Update only when checkbox is checked

I want to update a model - only lines where the checkox is clicked and insert a remark
View:
<%= form_tag update_fb_instruction_users_path, :method => :put do %>
<% #user_wishes.each do |u| %>
<%= u.user.name %>
<%= fields_for "instruction[]", u do |f| %>
<%= f.text_field :remark_tl %>
<% end %>
<%= check_box_tag "instruction_user_ids[]", u.id %>
<% end %>
Controller:
def update_fb
params[:instruction_user_ids].each do
#check = InstructionUser.update(params[:instruction].keys, params[:instruction].values).reject { |p| p.errors.empty? }
end
The issue there is that they all have the same name. So whatever value the last one is, that's what it will be in the request params.
It's a bit old, but you might want to check out the railscast here: http://railscasts.com/episodes/73-complex-forms-part-1. The basic idea is to use fields_for on top of each user object. I haven't done it myself before, otherwise i'd write a full solution :).

Resources