Preserve State of Pushed Radio Buttons - ruby-on-rails

How would one go about preserving the state of a "pushed" radio button after running a search? I'm using the Ransack gem to generate this search.
View:
<%= search_form_for #search do |f| %>
<strong>Location</strong></br>
<%= radio_button_tag("q[location_cont]", "Kentucky") %>
<%= label_tag "Kentucky" %></br></br>
<%= radio_button_tag("q[location_cont]", "Kansas") %>
<%= label_tag "Kansas" %></br></br>
<%= f.submit "Search" %>
<% end %>
Controller:
def index
#search = Job.search(params[:q])
#jobs = #search.result
end

If you were using form_tag, then radio_button_tag would be a valid field type to use. And you could use the third parameter to indicate if the button is selected or not:
<%= radio_button_tag("q[location_cont]", "Kentucky", params[:q] && params[:q][:location_cont] == "Kentucky") %>
<%= label_tag "Kentucky" %></br></br>
<%= radio_button_tag("q[location_cont]", "Kansas", params[:q] && params[:q][:location_cont] == "Kansas") %>
<%= label_tag "Kansas" %></br></br>
Since you are using the Ransack search_form_for, then you want to use radio_button and I think you'd want something like this:
<%= f.radio_button(:location_cont, "Kentucky") %>
<%= f.label :location_cont, "Kentucky", value: "Kentucky" %></br></br>
<%= f.radio_button(:location_cont, "Kansas") %>
<%= f.label :location_cont, "Kansas", value: "Kansas" %></br></br>

Related

Rails 6 pass variable to partial

I want to render a partial for each conversation. I have the following code:
# app/views/messages/_new.html.erb
<%= simple_form_for [#conversation, #message] do |f| %>
<%= f.text_area :body %>
<%= f.text_field :messageable_id, value: current_user.id, type: "hidden" %>
<%= f.text_field :messageable_type, value: "#{current_admin_user.class.name}", type: "hidden" %>
<%= f.submit "Send Reply" %>
<% end %>
I want to change value of both hidden text fields depends on passed variable. Something like:
# app/views/admin/conversations/_show.html.erb
<%= render 'messages/new', value: current_admin_user.id %>
With below code I'm getting an error Validation failed: Messageable must exist which means messageable_id = nil
Because you are not using the value you are passing to the partial.
Also, you need to pass both the variables
Try replacing the partial with the following
# app/views/admin/conversations/_show.html.erb
<%= render 'messages/new', messageable_id: current_admin_user.id, messageable_type: "#{current_admin_user.class.name}" %>
and use them
# app/views/messages/_new.html.erb
<%= simple_form_for [#conversation, #message] do |f| %>
<%= f.text_area :body %>
<%= f.text_field :messageable_id, value: messageable_id, type: "hidden" %>
<%= f.text_field :messageable_type, value: messageable_type, type: "hidden" %>
<%= f.submit "Send Reply" %>
<% end %>
NOTE: If you want the _new partial to render on its own as well
replace it with something like this:
<% messageable_id ||= current_admin_user.id %>
<% messageable_type ||= "#{current_admin_user.class.name}" %>
<%= f.text_field :messageable_id, value: messageable_id, type: "hidden" %>
...

Ruby on Rails ransack gem search

i want to implement a search that goes through multiple models.
Found this stackoverflow question here that uses ransack and tried it right away. But I can't seem to get it to work.
in my controller :
def search
#quotes = Quote.search(title_cont: q).result
#books = Book.search(title_cont: q).result
#users = User.search(username_cont: q).result
end
routes
get '/search', to: 'application#search'
view
<%= form_tag search_path, method: :get do %>
<%= f.label :title_cont %>
<%= f.search_field :title_cont %>
<%= text_field_tag :q, nil %>
<% end %>
You should use params[:q] instead of q. This should work
def search
#quotes = Quote.search(title_cont: params[:q]).result
#books = Book.search(title_cont: params[:q]).result
#users = User.search(username_cont: params[:q]).result
end
Also, f.label and f.search_field doesn't work with form_tag. You should use label_tag and search_field_tag instead
<%= form_tag search_path, method: :get do %>
<%= label_tag :title_cont %>
<%= search_field_tag :title_cont %>
<%= text_field_tag :q, nil %>
<% end %>
Why do you have two fields in your search form, you should only have one search field.
<%= form_tag search_path, method: :get do %>
<%= label_tag :title_cont %>
<%= search_field_tag :q %>
<% end %>

How to search more than one options on click on submit through ransack search

i want to search both category and location on click on search(Ransack)
my view
<%= form_tag location_path, :method=>'get' do %>
<%= select_tag :q, options_from_collection_for_select(Category.all, :id, :name, params[:q]) %>
<%= text_field_tag :q, nil,:placeholder=>"Tell us what you are looking for.." %>
<input type="submit" value="SEARCH" class="btn1 home-search-button" >
<% end %>
This is my view of this form
my searchcontroller is
def location
q = params[:q]
#key=q
#property = Property.ransack(location_or_category_name_cont: q).result(distinct: true)
end
this search searches only location not category,
on executing i am getting like this,
`url is like this : http://localhost:3000/location?utf8=%E2%9C%93&q=2&q=banglore`
command iam getting on executing
`
Here it is searching banglore as category name and location(it should search 'commercial' as category_name(which is under category_id:2) instaed of banglore'
Any help is appreciatable
now i am getting like this, seach query is wrong
`
<%= form_tag location_path, :method=>'get' do %>
<%= select_tag :category_id, options_from_collection_for_select(Category.all, :id, :name, params[:q]) %>
<%= text_field_tag :q, nil,:placeholder=>"Tell us what you are looking for.." %>
<input type="submit" value="SEARCH" class="btn1 home-search-button" >
<% end %>
try this
You are using same parameter name for location as well as for category and which is getting overridden.
not sure what version of ransack you are using but according to documentation ransack, following would fix the problem.
in your view, should use the search_form_for helper
<%= search_form_for #q do |f| %>
<%= f.label :category_name_cont %>
<%= f.select :category_name_cont, options_from_collection_for_select(Category.all, "name", "name", #q.category_name_cont) %>
<%= f.label :location_name_cont %>
<%= f.search_field :location_name_cont %>
<%= f.submit %>
<% end %>
and in your controller
def location
#q = Property.ransack(params[:q]).result(distinct: true)
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]' %>

Multiple radio button groups who send data in an array

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].

Resources