Multiple Select Sending Empty String to Rails Controller - ruby-on-rails

I have a tradie model that has many tradie_category models. In the form for the tradie I added the following:
= form_for #tradie do |tradie|
= tradie.select :tradie_category_ids, #categories, {}, { multiple: true }
In the HTML, the following is generated...
<form accept-charset="UTF-8" action="/add_a_tradies/create" class="new_tradie" method="post">
<input name="tradie[tradie_category_ids][]" type="hidden" value="">
<select class="form-control" id="tradie_tradie_category_ids" multiple="multiple" name="tradie[tradie_category_ids][]">
<option value="2">mechanic</option>
<option value="1">plumber</option>
</select>
</form>
I removed the authenticity token and submit button because it's not necessary with my problem. Whenever I do post the form, the params tradie_category_ids is returned as an empty string. What's wrong and how do I fix it so I can created multiple tradie categories for the tradie that is created.?

It seems this issue was also reported in another question: Why is the first element always blank in my Rails multi-select, using an embedded array?
They outline the specification for it and how it will be resolved in Rails 4. The second most popular response says how to apply a fix for it in the mean time.

Try this
= form_for #tradie do |tradie|
= tradie.select :tradie_category_ids, #categories, {}, { multiple: "true" or 'true' }
Note the quotes around
{ multiple: "true" or 'true' } in

Related

How to show more attributes in each element of the collection with collection_select

I have this with simple form:
= simple_form_for(Note.new, remote: true) do |f|
= f.collection_select :dance_id, current_user.dances.includes(:style).all,
:id, :style_name, {prompt: "Please select dance"}, {class: "form-control"}
Code generated:
<select class="form-control" name="note[dance_id]" id="note_dance_id">
<option value="">Please select dance</option>
<option value="39">Adowa Dance (Ghana)</option>
<option value="38">Adowa Dance (Ghana)</option>
<option value="37">Tango (Argentina)</option>
</select>
which works fine as it is displaying all dances by style_name as i want it. But i want to make a difference between two dances and want to also show level and date right next to each dance in the collection because there are many dances with same name bc of the style which is the same!
please help. any better way to do it with simple form helpers?
As stated in the discussion, you needed to declare a method in your model's rb file like this:
def custom_name
"#{style.name}. #{date}"
end
And change the :style_name parameter to :custom_name

undefined method `map' for "1,2":String

I need to pass an array in a params, possible? Values can be, for example, ["1","2","3","4","5"] and these are strings but needs to eb converted to integers later.
I use a react_component in between a rails form_for. The html is like this:
<input type="hidden" name="people_id" id="people_id" value={this.state.people} />
The people array looks like this:
How can I pass the array in the value of the hidden field? The server error I got was
Im trying to do something like this in a model:
ids = params[:people_id]
ids.map do |b|
Foo.create!(people_id: b.to_i)
end
If I ids.split(",").map I get symbol to int error.
Edit:
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Still not sure what the issue is as nothing works. Here is a minimal reproduction of my code:
This answer is my react component and that's how I add to the array. Still in the component, I have the hidden field:
<input type="hidden" name="[people_id][]" id="people_id" value={this.state.people} />
_form.html.erb:
<%= form_for resource, as: resource_name, url: registration_path(resource_name), :html => { :data => {:abide => ''}, :multipart => true } do |f| %>
<!-- react component goes here -->
<%= f.submit "Go", class: "large button" %>
<% end %>
The story is, guest can select few people during registration in one go. Those people will be notified when registration is complete. Think of it as "I am inviting these people to bid on my tender". Those numbers, in the array, are user_ids.
users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
# POST /resource
def create
super do |resource|
ids = params[:people_id].pop # logs now as "people_id"=>["1,2"]
resource.save!(ids.split(",").map |b| Foo.create!(people_id: b.to_i) end)
end
end
end
New error on line resource.save:
no implicit conversion of Symbol into Integer
Edit #2
If I only have, in the create method:
ids.split(",").map do |b|
resource.save!(Foo.create!(people_id: b.to_i))
end
It works! Foo is created two times each with the correct people_id.
Because I am creating more objects: Bar, I do not know how to do that in:
resource.save!(<the loop for Foo> && Bar.create!())
The flow must be:
Device creates the User
Foo is created with the loop
Bar is created
etc
It has to be done that way as an User object is created on the fly.
In Rails you use parameter keys with brackets on the end to pass arrays.
However you should not concatenate the values as a comma seperated list but rather send each value as a seperate param:
GET /foo?people_ids[]=1&people_ids[]=2&people_ids[]=3
That way Rails will unpack the parameters into an array:
Parameters: {"people_ids"=>["1", "2", "3"]}
The same principle applies to POST except that the params are sent as formdata.
If you want a good example of how this works then look at the rails collection_check_boxes helper and the inputs it generates.
<input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
<label for="post_author_ids_1">D. Heinemeier Hansson</label>
<input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
<label for="post_author_ids_2">D. Thomas</label>
<input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
<label for="post_author_ids_3">M. Clark</label>
<input name="post[author_ids][]" type="hidden" value="" />
Updated:
If you intend to implement you own array parameters by splitting a string you should not end the input with brackets:
<input type="hidden" name="[people_id][]" value="1,2,3">
{"people_id"=>["1,2,3"]}
Notice how people_id is treated as an array and the input value is the first element.
While you could do params[:people_id].first.split(",") it makes more sense to use the correct key from the get go:
<input type="hidden" name="people_id" value="1,2,3">
Also you don't really want to wrap the "root" key in brackets. Thats used in rails to nest a param key in a hash eg. user[name].

Is it possible to access the values of SimpleForm fields during input?

I'm using SimpleForm to collect a new object and I'd like to use values that the user has entered into the form to populate values in a nested form. Is there a way to do this?
So far I've been perusing the SimpleForm docs and poking around with pry when the form is active – without much luck… My next guess would be to try to get at the values with jQuery, but that is entirely new ground for me. Any suggestions would be most welcome.
<form id="get_value">
<input type="text" name="FirstName" ><br>
<input type="text" name="Email"><br>
</form>
<button>Get Value</button>
<div id="value"></div>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#value").text($("#get_value").serialize());
});
});
</script>
Try this. In your view:
<p id="output"></p>
<%= text_field_tag :foo, params[:foo], id: 'bar' %>
This will set your custom ID.
And then in your .js file:
$("#bar").keyup(function() {
var value = $(this).val();
$("#output").text(value);
}).keyup();
This will return a value of the input.
Source: http://api.jquery.com/val/

Passing array via hidden fields to rails

i have a hidden_tag like this in my form
<%= f.hidden_field :loc , {:multiple => true} %>
which renders to
<input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="">
currently am setting the business_loc value as a comma seperated string hoping rails would recognize when submit the form. But this is the value i got on the server side
"loc"=>["80.22167450000006,13.0454044"]
instead
"loc"=>[80.22167450000006,13.0454044]
how do i set the correct value in hidden field, so rails can understand it correctly.
You need to use multiple hidden fields, one for each element of the array of values.
For example:
<input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="80.22167450000006">
<input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="13.0454044">
...if you need code to dynamically add these with JS, here's a jQuery example:
var field = $('<input id="business_loc" multiple="multiple" name="business[loc][]" type="hidden" style="color: rgb(175, 175, 175); " value="13.0454044">');
var form = $('#your-form-id');
form.append(field);
I've found text_area's to make things work without having to add a bunch of hidden forms. Just set the value of the text area to something that looks like [1,31,51,61] and it should work, assuming in your model you have serialize :var
I had this same problem recently. My solution was to handle it on the server side by simply splitting the array at the comma. In my case it looks like this:
# thing_that_has_many_objects.rb <-- showing custom setter method from the model because my example involves using a virtual attribute
# params[object_ids] = ["1,2,3,4,5"] <-- from the form - note the format of array with only one element
def objects=(object_ids)
split_array = object_ids[0].split(',')
split_array.each do |id|
self.objects.build(object_id: id)
end
end

How do I allow a user to select zero options from a multiple selection box in rails?

I have a settings model with a column options, and set it to serialize with serialize :options. In my view, I have a multiple selection box, using select("settings", "options", ['option1','option2','option3'], {}, :multiple => true) which works fine so long as the user selects at least one option. However, if they don't select any options, no options are submitted and so the options aren't updated.
How do I allow the user to select zero options from a multiple selection box in rails?
That has nothing to do with rails: html form won't send such parameter to server if nothing is chosen in 'select' element. But you should be able to fix it in controller. Something like this
if params[:settings] == nil
params[:settings] = [];
end
Not sure if there's more elegant solution.
Add a hidden field after the select box, that posts an empty value to "settings[options]"
It's same trick that rails uses to make sure unchecked checkboxes get posted as false.
I do not like assuming a value is empty if the attribute is not posted. It breaks the way Rails expects to update attributes, and it can present problems if you are using your controller actions also for APIs as well as HTML. My preferred way of handling this is by adding a hidden input field before multiselects.
<input type="hidden" value="" name="parent_model[my_attribute_ids][]">
If you use JQuery you can automate the addition of these hidden input fields:
$('select[multiple="multiple"]').each(function(i){
$(this).before('<input type="hidden" name="'+this.name+'" value="" />')
});
I realize this answer is not very timely, but I hope this will help someone with a similar question.
You could provide a "None" option.
Example from: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'}, {:multiple => true})
Would become
<select name="post[person_id]" multiple="multiple">
<option value="">None</option>
<option value="1">David</option>
<option value="2" selected="selected">Sam</option>
<option value="3">Tobias</option>
</select>

Resources