I want to get checkboxs values in order to set parameters of an http request to a restful API. I am new to web development and I am having troubles with ruby on rails 5.1
Basically, I have two views: - one where you can check/uncheck two checkboxs and then press a button which makes some API calls and send you to the second view. - the second is just for displaying the API informations.
In the first view, I succeed to do the button that "makes" the API call, and the API informations are correctly displayed in the second. I now have trouble to add parameters, with the two checkboxs, to the request. My checkboxs are always returning me "nil" in params[].
I referred to this section of the rails doc: https://guides.rubyonrails.org/form_helpers.html#helpers-for-generating-form-elements and to several other stackoverflow questions without understanding what I missed.
here is the controller code:
class IndexController < ApplicationController
include HTTParty
def button_state
end
def display_state
require 'open-uri'
require 'json'
# this part is for checking my checkbox values, they are always nil
@t1 = params[:param_name1]
@t2 = params[:param_name2]
@url_test = 'www.bkbahbqiv.com/tamere?test1=' << @t1.to_s << '&test2=' << @t2.to_s
# other part related to the API calls
...
end
end
and here is my view code:
<h1>Check/Uncheck the boxes and click on the button to get results</h1>
<div class="checkbox">
<td>
<%= label_tag(:param_name1, "test1") %>
<%= check_box_tag(:param_name1) %>
</td>
</div>
<div class="checkbox">
<td>
<%= label_tag(:param_name2, "test2") %>
<%= check_box_tag(:param_name2) %>
</td>
</div>
<%= button_to "go to display_state", display_state_path, :method => :get %>
I will just add that I don't want to store the checkbox values in a database.
Thanks in advance for anyone helping me :)
The below suggestion will definitely work for you. Please change your view something below You need to used to form tag for your problem solution
<%= form_tag display_state_path, :method => 'get' do %>
<div class="checkbox">
<td>
<%= label_tag(:param_name1, "test1") %>
<%= check_box 'param_name1', 'result', {}, 1, 0 %>
</td>
</div>
<div class="checkbox">
<td>
<%= label_tag(:param_name2, "test2") %>
<%= check_box 'param_name2', 'result', {}, 1, 0 %>
</td>
</div>
<%= submit_tag "go to display_state" %>
<% end %>
And you can access the checkbox values in the params
params[:param_name1][:result]
params[:param_name2][:result]