Multiple radio button groups who send data in an array - ruby-on-rails

I've tried the following:
<%= form_for .... do |f| %>
<%= f.label "test1" %>
<%= radio_button_tag 'value[]', "1" %>
<%= radio_button_tag 'value[]', "2" %>
<%= f.label "test2" %>
<%= radio_button_tag "value[]", "1" %>
<%= radio_button_tag "value[]", "2" %>
<%= f.submit "test_send" %>
<% end %>
In my controller i then have an array.
The problem now is that i can only select one of this four, but i want select them after group. With text_fields it works fine but with radio buttons it doesn't work.
Then i tried something like:
<%= form_for .... do |f| %>
<%= f.label "test1" %>
<%= radio_button_tag 'value[]', "1", :id => "btn-grp-1" %>
<%= radio_button_tag 'value[]', "2", :id => "btn-grp-2" %>
<%= f.label "test2" %>
<%= radio_button_tag "value[]", "1", :id => "btn-grp-3" %>
<%= radio_button_tag "value[]", "2", :id => "btn-grp-4" %>
<%= f.submit "test_send" %>
<% end %>
to have unique id's but still the same problem.
What i need is to have a unique name for every group like:
<%= form_for .... do |f| %>
<%= f.label "test1" %>
<%= radio_button_tag 'value[1]', "1", :id => "btn-grp-1" %>
<%= radio_button_tag 'value[1]', "2", :id => "btn-grp-2" %>
<%= f.label "test2" %>
<%= radio_button_tag "value[2]", "1", :id => "btn-grp-3" %>
<%= radio_button_tag "value[2]", "2", :id => "btn-grp-4" %>
<%= f.submit "test_send" %>
<% end %>
But how can i get the params now?
My controller contains ony code for test what is send:
...
def create
flash[:success] = valueset_params[:value]
redirect_to root_path
end
private
def valueset_params
params.permit({value: []})
end
...
Hope you understand what i mean. (I have to change the name of the radio buttons and i still want receive the full array in my controller).
Just ask if you don't.
Thanks for any solution propose.
Edit Another question:
I have something like #topics where i have multiple topics inside.
Now i want loop them (i know how this works) and write variable values inside the []
<%= form_for .... do |f| %>
<%= f.label 'test1' %>
<%= radio_button_tag 'value[#topic1.id]', '1' %>
<%= radio_button_tag 'value[#topic1.id]', '2' %>
<%= f.label 'test2' %>
<%= radio_button_tag 'value[#topic2.id]', '1' %>
<%= radio_button_tag 'value[#topic2.id]', '2' %>
<%= f.submit 'test_send' %>
<% end %>

Try this and let me know what happens:
<%= form_for .... do |f| %>
<%= f.label 'test1' %>
<%= radio_button_tag 'value[group_one]', '1' %>
<%= radio_button_tag 'value[group_one]', '2' %>
<%= f.label 'test2' %>
<%= radio_button_tag 'value[group_two]', '1' %>
<%= radio_button_tag 'value[group_two]', '2' %>
<%= f.submit 'test_send' %>
<% end %>
You should be able to get your data in the controller with params[:value] and then params[:value][:group_one] and params[:value][:group_two].

Related

How to insert new line breaks using form_for with collection_check_boxes

I use the following form_for at one point in my code to display a series of topics for users to select from.
<%= form_for #user do |f| %>
<%= f.collection_check_boxes(:topic_ids, Topic.all.sample(50).each, :id, :topic_name) %>
<%= f.submit %>
<% end %>
When it renders on the page, the boxes are all one-after-the-other. How can I separate them so that there is one check box per line?
from reference here
It is possibly to customize the way the elements will be shown by giving a block to the method as sample below from your code above
<%= form_for #user do |f| %>
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name do |b| %>
<%= b.label(class: "check_box") do %>
<%= b.check_box(class: "check_box") %>
<%= b.object.topic_name %></br>
<% end %>
<% end %>
<%= f.submit %>
<% end %>
You may of course render HTML inside the block:
<%= form_for #user do |f| %>
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, : topic_name do |b| %>
<div class="my-checkbox-wrapper">
<%= b.label(class: "foo") do %>
<%= b.object.topic_name %>
<%= b.check_box(class: "bar") ></br>
<%end%>
<%= f.submit %>
<%end%>
You can have a look at this example
It is very broad question. In short, using CSS.
For example using Bootstrap 4:
<%= form_for #user do |f| %>
<div class="form-check">
<%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name, class: 'form-check-input' %>
</div>
<%= f.submit %>
<% end %>

Passing information to a controller through params (not saving it to the db). How to put it inside a hash like the users params hash

I have information that is passed to a controller method but isn't saved to the DB. I want to access this information, that is passed to the controller method as a whole hash, but it is all individual data as you will see below.
Here is the params:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0O7pbNNrddHCyPL9B/avUUD85574rFBfS57h+aWKK/mBakPSn5iHJKHhPmvuJVfyWxjBsAQn2kagwkTOALEKRg==", "page"=>{"content_top"=>"", "content_bottom"=>""}, "header1"=>"iijijij", "column1"=>"ijijijij", "header2"=>"", "column2"=>"", "header3"=>"", "column3"=>"", "header4"=>"", "column4"=>"", "commit"=>"Save", "guide_id"=>"dungeon-boss", "category_id"=>"heroes", "id"=>"link-skill"}
As you can see there is a page hash and after, it is header1 column1 header2 column2... and so on. With the header1 info, I'm trying to put it inside a params hash like the page hash has for the values passed in it. So it's like "table" =>{"header1"=>"iijijij", "column1"=>"ijijijij", "header2"=>"", "column2"=>"", "header3"=>"", "column3"=>"", "header4"=>"", "column4"=>""}
I'm sure there is something I need to add to the form so it know to group them like this. Here is the form I currently have
<% if (current_user.mod_of_game?(#guide) unless current_user.nil?) %>
<%= form_for([#category, #page], url: update_pages_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :content_top, "Top Content" %>
<%= f.text_area :content_top, :class => 'editor' %>
<%= f.label :content_bottom, "Bottom Content" %>
<%= f.text_area :content_bottom, :class => 'editor' %>
<!-- to be in one hash when passed -->
<%= text_field_tag :header1 %>
<%= text_field_tag :column1 %>
<%= text_field_tag :header2 %>
<%= text_field_tag :column2 %>
<%= text_field_tag :header3 %>
<%= text_field_tag :column3 %>
<%= text_field_tag :header4 %>
<%= text_field_tag :column4 %>
<!-- end -->
<%= f.submit "Save" %>
<% end %>
I cant find what I need to add to make the text_field_tag data all be in one hash when passed.(the text_field_tag is purposely not being saved to the DB on form submit, it just needs to be passed to the method and grouped inside a hash)
how about using fields_for like this
<%= form_for([#category, #page], url: update_pages_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :content_top, "Top Content" %>
<%= f.text_area :content_top, :class => 'editor' %>
<%= f.label :content_bottom, "Bottom Content" %>
<%= f.text_area :content_bottom, :class => 'editor' %>
<!-- to be in one hash when passed -->
<%= f.fields_for :table do |t| %>
<%= t.text_field_tag :header1 %>
<%= t.text_field_tag :column1 %>
<%= t.text_field_tag :header2 %>
<%= t.text_field_tag :column2 %>
<%= t.text_field_tag :header3 %>
<%= t.text_field_tag :column3 %>
<%= t.text_field_tag :header4 %>
<%= t.text_field_tag :column4 %>
<% end %>
<!-- end -->
<%= f.submit "Save" %>
<% end %>
You can simply use the array way if you want to send the data that doesn't belong to the model, in params hash inside page key. Here's how:
<%= text_field_tag 'page[header1]' %>
Not only this, if you would like to use another key, you can use that as well like <%= text_field_tag 'custom_key[header1]' %>

Change Simple_Form Submit Button Action?

I've created the following SimpleForm for my Rails application. I want the submit button to take the data selected in each of the fields, and carry it over to the next screen. Does anyone know how I can change the property of the SimpleForm submit button? Right now it just seems to create something? I'm a little confused by what it's default action is.
<div class="infoheaders">1. Question
<%= simple_form_for #category do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>
<div class="grey-text-subheader">Device: (optional)</div>
<%= f.check_box :Apple %> <%= f.label "Apple" %>
<%= f.check_box :iPhone %> <%= f.label "iPhone" %>
<%= f.check_box :iPad %> <%= f.label "iPad" %>
<%= f.check_box :Mac %> <%= f.label "Mac" %>
<%= f.check_box :Android %> <%= f.label "Android" %><br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %>
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>
<%= f.check_box :Windows %> <%= f.label "Windows" %>
<%= f.check_box :Google %> <%= f.label "Google" %>
<%= f.button :submit, 'Submit' %>
<% end %>
</div>
simple form for action can be changed to the action(use 'url' option), we want for example,
<div class="infoheaders">1. Question
<%= simple_form_for #category, :url => url_for(:action => 'your_action', :controller => 'your_controller'), :method => 'post' do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>
</div>
<div class="grey-text-subheader">Device: (optional)</div>
<%= f.check_box :Apple %> <%= f.label "Apple" %>
<%= f.check_box :iPhone %> <%= f.label "iPhone" %>
<%= f.check_box :iPad %> <%= f.label "iPad" %>
<%= f.check_box :Mac %> <%= f.label "Mac" %>
<%= f.check_box :Android %> <%= f.label "Android" %>
<br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %>
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>
<%= f.check_box :Windows %> <%= f.label "Windows" %>
<%= f.check_box :Google %> <%= f.label "Google" %>
<%= f.button :submit, 'Submit' %>
<% end %>
</div>
In your controller, you will get the params that were send through the form,
class Yourcontroller
def your_action
// render text: params and return false
if params[:category][:ipad].present?
#checked = params[:category][:ipad]
end
if params[:category][:ipad].present?
#checked = params[:category][:android]
end
if params[:category][:ipad].present?
#checked = params[:category][:samsung]
end
/* check the params and assign to the variables */
// render text: params[:category] and return false
end
end
Now create a your_action.html.erb and you will get values there.
<div>
The checked valus is <%= #checked %>
</div>
Thats it.
Replace Your form with the following
<div class="infoheaders">1. Question
<%=form_for #category, :url=>"/register", do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>
<div class="grey-text-subheader">Device: (optional)</div>
<%= f.check_box :Apple %> <%= f.label "Apple" %>
<%= f.check_box :iPhone %> <%= f.label "iPhone" %>
<%= f.check_box :iPad %> <%= f.label "iPad" %>
<%= f.check_box :Mac %> <%= f.label "Mac" %>
<%= f.check_box :Android %> <%= f.label "Android" %><br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %>
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>
<%= f.check_box :Windows %> <%= f.label "Windows" %>
<%= f.check_box :Google %> <%= f.label "Google" %>
<%= f.button :submit, 'Submit' %>
<% end %>
</div>
And in routes Add the following lines
get "/register"=> "controller#action"

Extend forms without page reload, rails

I'm searching for a solution to extend my form without page reload.
First I tried to render a partial with coffee or javascript, but escape_javascript didnt work.
Here's the view
<%= form_for #recipe = current_user.recipes.build do |f| %>
<%= f.label :name, "Recipe Name" %>
<%= f.text_field :name %>
<%= button_to "Add Ingredient", '#', class: "btn btn-lg btn-primary", id: "add" %>
<p></p>
<%= f.submit class: "btn" %>
<% end %>
The form above should be extented with following partial by every click on the button
<div id="recipe-ingredients">Quantity
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= render 'quantity_fields', f: quantity %>
<% end %>
</div>
_quantity_fields
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
This approach did not work (recipes.js.erb)
$(document).ready(function() {
$("#add").click(function() {
$("p").append("<%= escape_javascript
render(:partial => 'quantities') %>");
});
});
There's a workaround (see below) but I'm searching for a better solution.
$(document).ready(function() {
$("#workout-week").append(<%= escape_javascript(
Haml::Engine.new(File.read(File.join(Rails.root,'app/views',
'_show_period.html.haml'))).render(Object.new, period: #period)) %>);
});
A second approach is to write following lines in Coffee or JavaScript:
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
<% end %>
I'm a newbie so take this with a grain of salt, but have you tried using render :foo instead of redirect_to :foo in the appropriate controller function?
I solved it with cocoon
<%= form_for #recipe do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.fields_for :quantities do |quantity| %>
<%= render 'quantity_fields', :f => quantity %>
<% end %>
<div class="links">
<%= link_to_add_association 'add', f, :quantities %>
</div>
</div>
<%= f.submit %>
<% end %>

RoR field set on form_for

How do you add a field set to a form_for method?
You can use field_set_tag. For example, using a generic 'user' object
For Rails 2.3.x
<% form_for(#user) do |f| %>
<% field_set_tag 'Name' do %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<% end %>
<% end %>
And for Rails 3.0.0:
<%= form_for(#user) do |f| %>
<%= field_set_tag 'Name' do %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<% end %>
<% end %>
You need to have a new object or get an existing object from your dc since it is a 'form for' and then you create a form builder f and call methods on that form builder such as the following:
<% form_for(#object) do |f| %>
<%= f.text_field :method_name %>
<% end %>

Resources