How to post grouped input data within a form in Rails - ruby-on-rails

I have a form that contains 4 input fields.
<%= semantic_form_for #some_model, html: {class: "horizontal-form"} do |f| %>
<%= f.input :person_id %>
<%= f.input :car_id %>
<%= f.input :person_id %>
<%= f.input :car_id %>
<%= f.actions do %>
<%= f.action :submit, button_html: {class: "btn btn-primary"} %>
<% end %>
<% end %>
As you can see the input fields are identical.
I want to group these fields so they can be posted as an array like this:
[ [3838, 9090], [2938, 893] ]
Ang then i can be able to loop this array and fetch the data i need from an external api.
How can i achieve this?

version of HTML
<input type="textbox" name="person_id[]", value="person A">
<input type="textbox" name="car_id[]", value="car A">
<input type="textbox" name="person_id[]", value="person B">
<input type="textbox" name="car_id[]", value="car B">
OR
= simple_form_for # some_model do |f|
= f.input_field :name, multiple: true
= f.input_field :name, multiple: true
= f.submit

Could you not create another field on your Model to hold the array.
Then create a Private controller method that runs when the form is saved and appends all the values to the array?

Related

I am using Rails serialize to store Array of data, but my form is not saving array of data

a. I create a migration
def change
add_column :tournament_matches, :team_a_sets, :text
add_column :tournament_matches, :team_b_sets, :text
end
b. I added Serialize in model
class TournamentMatch < ApplicationRecord
serialize :team_a_sets, Array
serialize :team_b_sets, Array
end
c. I allow strong params in my controller
def tennis_params
params.require(:tournament_match).permit(:match_result, team_a_sets: [], team_b_sets: [])
end
d. my form looks like this:
<%= form_with model: #tournament_match, url: update_tennis_game_result_tournament_match_path(#tournament_match), method: :patch do |f| %>
<%= f.label :team_a, 'Team A' %>
<%= f.number_field 'team_a_sets[]' %>
<%= f.number_field 'team_a_sets[]' %>
<%= f.number_field 'team_a_sets[]' %>
<%= f.number_field 'team_a_sets[]' %>
<%= f.number_field 'team_a_sets[]' %><br>
<%= f.label :team_a, 'Team B' %>
<%= f.number_field 'team_b_sets[]' %>
<%= f.number_field 'team_b_sets[]' %>
<%= f.number_field 'team_b_sets[]' %>
<%= f.number_field 'team_b_sets[]' %>
<%= f.number_field 'team_b_sets[]' %><br>
<%= f.submit 'Submit', class: 'btn btn-primary btn-block' %>
<% end %>
e. my controller looks like this
def update_tennis_game_result
tournament_match = TournamentMatch.find(params[:id])
tournament_match.update_attributes(tennis_params)
redirect_to tournament_path(tournament_match.tournament.id)
end
But when I am submitting my form, I am getting empty array for team_a_sets[] and team_b_sets[]. What I am doing wrong ? I want to store games results in team_a_sets and team_b_sets as array.
My params after submitting my form looks like this:
Parameters: {"authenticity_token"=>"Rd/3zOIv0Ldmq/qxlIDPcYzcAuhPmz20JYUpHedeeQSwtp4gf8RhYfXcLdhrlVdDQxGjHtCK6Cq0FB6FNWtdPQ==", "tournament_match"=>{"match_result"=>"home_win", "team_a_sets"=>[], "team_b_sets"=>[]}, "commit"=>"Submit", "id"=>"296b8505-c116-4e08-a48a-53366eac93b0"}
It looks like form_helper check the suffix [] to detect multiple fields.
But your form input
<%= f.number_field 'team_a_sets[]' %>
will generate html_tag like this
input type="number" name="tournament_match[team_a_sets[]]" id="tournament_match_team_a_sets[]"
in which the name hasn't the suffix [].
you could try this instead:
<%= form.number_field 'team_a_sets', multiple: true %>
the html_tag is generated in this case
input multiple="multiple" type="number" name="tournament_match[team_a_sets][]" id="tournament_match_team_a_sets"
in which the html_name has suffix [].

simple_form adding class that is conflicting with my css. Need to remove class

I'm using Simple_form with Rails 5 and bootstrap 4. I'm having an issue where i've set my datatype to be :text
My bootstrap form brings in the class text as it should however this is making my form hide somewhere beneath something on the page. When I remove the text class and add a class of string it appears in the view.
whats the best option here? Just try to change the text class to be string class?
or try amend the text class simple form is calling to something else text_new class or something so it doesn't conflict with something somewhere else?
here's the code I'm getting and result i'd like. I've tried adding
html: { class: "string"}
but this does nothing for me.
I essentially need the three instances of class "text" changed to "string" or better yet have it removed or changed to different name in a default setting please.
<div class="form-group text required testimonial_quote">
<label class="control-label text required" for="testimonial_quote">
<abbr title="required">*</abbr> Quote</label>
<textarea class="form-control text required" name="testimonial[quote]" id="testimonial_quote"></textarea></div>
My Simple form code
<%= simple_form_for(#testimonial) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :quote %>
<%= f.input :received %>
<%= f.association :course, label_method: :header %>
<%= f.input :featured %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Without you posting your existing simple_form code I can't give you the exact answer. That said I think the line you are looking for is:
<%= input_html: { class: 'string' } %>
Example from the simple_form docs:
<%= simple_form_for #user do |f| %>
<%= f.input :username, input_html: { class: 'special' } %>
<%= f.input :password, input_html: { maxlength: 20 } %>
<%= f.input :remember_me, input_html: { value: '1' } %>
<%= f.button :submit %>
<% end %>
Please try as below
html: {...} for form tag
input_html: {...} for input tag.
class: "..." for submit tag
For example:
<%= simple_form_for #product, html: { class: "ex-product-form" } do |f| %>
<%= f.input :name, input_html: { class: 'p-name' } %>
<%= f.button :submit, class: "btn btn-default" %>
<% end %>

create record based on collection_select dropdown

I am trying to create playlist records based on the dropdown selection. I'm going to put this in a bootstrap divided dropdown once functional. For now I have a dropdown and a link. The link does the thing, creates a new playlist obj with the values from the video it was clicked on, everything is a PORO until added as a playlist obj, where the video is persisted to the DB as a video obj. What I am wanting to do is, if you want to add a video to as a playlist obj, that you see a list of associated playlist names. When you click on the name, it does the same thing as the link_to but takes the name parameter along with it to create the new record. Thanks for any help.
PS. if you have tips on implementing the bootstrap that'd be wicked helpful too :D
<%= form_tag :playlists, {action: 'create'} do %>
<%= collection_select :playlist, :name, current_user.playlists, :id, :name, class: 'dropdown-item' %>
<%= link_to 'Create Playlist', new_playlist_path(etag: video.etag, video_id: video.video_id, user_id: current_user.id,
img_high: video.img_high, img_default: video.img_default,
title: video.title, description: video.description), class: 'dropdown-item' %>
<% end %>
Basically, what I'm trying to do is...be able to do what the link_to is doing but also take along the playlist name
Bind to the onchange event of the the select and trigger it's form to submit.
<%= collection_select :playlist, :name, current_user.playlists, :id, :name, class: 'dropdown-item', onchange: "this.form.submit()" %>
Here's an example:
<form action="https://google.com/">
<select name="q" onchange="this.form.submit()">
<option value="abc">Option 1</option>
<option value="123">Option 2</option>
</select>
</form>
Was finally able to get the passing the params, I still have to use a button as couldn't get the onchange: to work yet.
<%= form_tag :playlists, {action: 'create'} do %>
<%= hidden_field_tag :etag, video.etag %>
<%= hidden_field_tag :video_id, video.video_id %>
<%= hidden_field_tag :img_high, video.img_high%>
<%= hidden_field_tag :img_default, video.img_default %>
<%= hidden_field_tag :title, video.title %>
<%= hidden_field_tag :published_at, video.published_at %>
<%= hidden_field_tag :description, video.description %>
<%= hidden_field_tag :user_id, current_user.id %>
<%= collection_select :playlist, :name, current_user.playlists, :name, :name, {class: 'dropdown-item', onsubmit: 'this.form.submit()'} %>
<%= submit_tag 'Add', method: 'post' %>
Wasn't aware I could use the hidden_field_tag there like that :P

Rails 4 parameters hash not working correctly on update action

When I create a search the correct parameters are coming in fine in the rails 4 params hash via params[:search] but on the update action some of my parameters are not working via params[:search]. When I looked at the request for the update action I found what I was looking for( code directly below) but when using rails params hash I am only getting 4 of them( latter code ). Any ideas on how to fix this?
I am not talking about using the search_params function. I am talking about when debugging the app you can make sure the parameters are in the hash. If they are not in the params hash they will not show up no matter what your code looks like in search_params
"search"=>
{"gender"=>"2",
"height_low"=>"60",
"height_high"=>"70",
"weight_low"=>"80",
"weight_high"=>"110",
"age_low"=>"18",
"age_high"=>"33",
"city"=>"fg",
"province_state"=>"fg",
"country"=>"CA"}
"search"=>
{"gender"=>"2", "city"=>"fg", "province_state"=>"fg", "country"=>"CA"}
form
<%= form_for #search, url: {action: "update"}, html: { method: "post"} do |f| %>
<%= f.label :gender, 'Gender:' %>
<%= f.select(:gender, attractions, :selected => #search.gender) %>
<br />
<%= f.label :height_low, 'Height between:' %>
<%= f.select(:height_low, heights) %>
<%= f.select(:height_high, heights) %>
<br />
<%= f.label :weight_low, 'Weight between:' %>
<%= f.select(:weight_low, (80..300).to_a.map{|n| [n, n]}) %>
<%= f.select(:weight_high, (80..300).to_a.map{|n| [n, n]}) %>
<br />
<%= f.label :age_low, 'Age between:' %>
<%= f.select(:age_low, (18..100).to_a.map{|n| [n, n]}) %>
<%= f.select(:age_high, (18..100).to_a.map{|n| [n, n]}) %>
<br />
<%= f.label :city, 'City:' %>
<%= f.text_field :city, required: true %>
<%= f.label :province_state, 'Province or State:' %>
<%= f.text_field :province_state, required: true %>
<%= f.label :country, 'Country:' %>
<%= f.country_code_select :country, [[ 'United States', 'US' ], [ 'Canada', 'CA' ]] %>
<div><%= f.submit "Search" %></div>
search parameters in controller
private
def search_params
params.require(:search).permit(:age_low,:age_high,:weight_low,:weight_high,:height_low,:height_high,:gender,:screen_name,:city,:province_state,:country)
end
This isn't much to go on since there isn't any background information to how you are using this has. You might only be permitting specific values in your parameter hash? For example
def search_params
params.require(:search).permit(:gender, :city, :province_state, :country)
end
Maybe provide a little more information.

How do I translate an HTML input field to "simple_form"?

I have the following HTML input field:
<input type="text" class="login-field" value="" placeholder="Enter your name" id="login-name" />
<label class="login-field-icon fui-user" for="login-name"></label>
I want to translate it into my simple_form field so it will have the same properties like classes, id's etc.
<%= f.input :email%>
Is there a way to add HTML properties inside the simple_form field?
Extracted from simple_form documentation on Github:
<%= simple_form_for #user do |f| %>
<%= f.input :username, label: 'Your username please' %>
<%= f.input :password, hint: 'No special characters.' %>
<%= f.input :email, placeholder: 'user#domain.com' %>
<%= f.input :remember_me, inline_label: 'Yes, remember me' %>
<%= f.button :submit %>
<% end %>
By default it contains labels.
Also, specific options in input call will overwrite the defaults:
<%= f.input :username, input_html: { class: 'special' } %>
Have a look at their Github page, everything is there.

Resources