Multiple Dynamic Controls - Results as Array? - ruby-on-rails

I'm building an editor that works with .CSV files. I have the application importing the file fine, but now I want the user to be able to select a few columns to work with.
I display the top 5 columns of the file in an HTML table, and in the table TH tag I'm creating some checkboxes at the top of the table like this:
It ends up looking like this:
All of this is wrapped up in a form and when it gets submitted the params contain the IDs of the checked checkboxes/columns.
"0"=>"0",
"3"=>"3"
I want to find out which columns have been selected, but to my mind, scraping through the params and trying to work out which columns is a tad messy.... is there a way to get the selected checkboxes back as an array so I can just iterate through them? The number of columns is variable.
Solved!
Changed the checkbox generation to this:
and all of the selected columns go into an array called selected_columns. Simple!

Changed the checkbox generation to this:
<% 0.upto(#column_index_max) do |column_index| %>
<%= check_box_tag "selected_columns[]" , column_index %>
<% end %>
and all of the selected columns go into an array called selected_columns. Simple!

Related

Ruby on Rails - Update multiple data in a table with select (bulk edit)

(Rail 5 beta 3)
I have a table on an index page (action) of a view with around 15 columns. Some of them are for text and some of them are for integers only. Every entry of this list (table) will be filled out by a 'form_for' form (in new or edit action).
For editing or deleting there are links with each list entry in the index view leading to the corresponding show, edit or destroy actions. This all works well. Some of these entries are entered by a select with pulldown on the new or edit view. This works well, too.
But if one of these selects should be changed for more than one entry in the list it takes too much time to click on 'edit', change the select and click on submit at each list item. To make this a better user experience I would like to be able to change the selects in the list (table) directly. It would be good to have the select/pulldown in place. The change of the state or choosen entry should than be saved in place as well or with an extra button ("save changes") above/below the table.
To say it in short:
I want to update multiple entries in a table in an index view without editig each single entry via edit view. The dates will be changed by a select and the data should be saved by a submit button on this page
Has anybody an idea how I can solve this?
Try best_in_place gem. It can solve the problem you have quoted. here are some links to it
https://github.com/bernat/best_in_place
http://railscasts.com/episodes/302-in-place-editing?view=asciicast
Your original text wasn't that confusing to me...
You want what's called a bulk edit feature. Easiest way would be to set up a new target at the controller level specifically to handle this request. I'll give you the pseudocode and it should be easy enough to fill in the blanks, but essentially:
Create a "bulk edit" form (the drop down select menu above the table)
Create a controller method to handle the bulk edit (controller#bulk)
Update routes.rb to direct a URL to that new method
Handle the bulk update in the controller and redirect back to index upon completion (cheap way of updating the page after editing is done).
Note: I'm assuming your model name is "Resource" because you did not specify what it actually was
On your index page, you want HTML like:
<form method="POST" action="/resources/bulk">
<select name="bulk_value">...</select>
</form>
On change/form submit/whatever, submit that form.
If you're using resourceful routing, you can hook this into config.rb via:
resources :resources do
post :bulk, on: :collection
end
Otherwise, hook the route however you see fit.
Then, in your controller:
def bulk
value = params[:bulk_value]
Resource.update_all value: value
redirect_to {resources_path}
end
Now, you said index view, so I am assuming you want all. But if you just want a subset, you can pass the preferred IDs along with the form as a hidden field, then use a where clause to filter, i.e.
Resource.where(id: parmas[:id_array]).update_all(value: value)
Anyway, that should get you down the right path.

Rails Populating SELECT options from database

I feel like this is basic but can't find details anywhere.
I have a basic application created by generating scaffold.
There is form already built into this that enters data. I have a SELECT (drop down) box in the form. I would like to be able to have the OPTIONS in this select box be pulled from a database. Ideally, the end user would be able to add and edit these options.
I have no idea how to link that SELECT field in my form to where the OPTIONS will be STORED.
Can someone point me in the right direction as far as terminology as what to research? I'm coming up blank. I feel like this must be a common thing to do..
As ByScripts' link includes, here is the script that worked for me (for those where time is not a luxury):
<%= collection_select(:lang_id, 0, Language.all, :id, :name) %>
Where Language is a table with one column called 'name' and an auto-assigned column id; :lang_id is the name of the element and 0 is the default selected index when the page loads.

How to get params from checkbox in rails3 controllers

Here is my codes:
<%=form_tag('/user_group_follows/follow',:id=>'follow_select_form',:remote=>true,:method=>:get) do %>
<p>You want to add this user to?</p>
<%=hidden_field_tag 'user_id',#user.id%>
<%#user.user_groups.each do |ug|%>
<%=check_box_tag 'user_group_id',ug.id,false,{:id=>'user_group_id_'+ug.id.to_s}%><%=ug.name%><br/>
<%end%>
<%end%>
//using jquery-ui, so there is no submit button....
I wanna the user to make a multiple choice to decide which groups that he/she would like to add into the following list.
So I made several checkboxes with the same name as 'user_group_id' and different ids.
I could successfully get the params throught params[:user_group_id], if the user only checked one box. But if he truly checked several of them, how to get this value set in controller? In this circumstance, params[:user_group_id] could only get one of them. And I quite believe codes like: params[:user_group_id_+XXX.id] is not going to work....
If you name them with id's like user_group_id['+ug.id+'], I think you should get params like params[:user_group_id] which should contain an array of all the id's of groups that were checked.
Something like this, not sure exactly, but basically you want to name your fields such that they are grouped into an array naturally, by virtue of how they are named:
<%=check_box_tag 'user_group_id['+ug.id']',ug.id,false,{:id=>'user_group_id_'+ug.id.to_s}%><%=ug.name%>
So, params[:user_group_id].first would contain the id of the first checkbox that was selected.
if you go in this way <%=check_box_tag 'user_group_id[]'%> it's returning array of selected ids,
<%=check_box_tag 'user_group_id',ug.id, params[:user_group_id].try(:include?,ug.id.to_s),{:id=>'user_group_id_'+ug.id.to_s}%>

Ruby on rails: Avoid drop down if values are already selected

I am pretty new to Ruby on rails. I have one query
I have one view (say view1) in which I am showing a drop down. For that I have passed and array and populating the drop down using that array like dis
<td><=% select_tag, options_for_select(#businessApprovers)></td>
So when I submit the form it goes to an action which intrun renders another view which has 5 tabs in body and each tab has a partial view. one of them calls my previous view view1. Now when it calls the view1 it again shows the drop down. instead it should show only one value and that too non editable.
we are having some other drop down too but they have hard coded values. We are doing that like this:
<td><=% f.field :contries, :condition_select, [abc,pqr] ....
and above thing is working fine. For that it is not showing drop down.
So I wanted to know how to avoid that drop down. Also what is the use of "f.field" because I removed that and from then on it is causing this problem.
Firstly, field is for CSV, text_field is a thing. Is that what that should be? Also, I'm hoping that's not exact code.
Instead of:
<td><=% f...
It should be:
<td><%= f...
That aside, if you're simply looking to display a select field when no selection has yet been made and simply text when it DOES have a value, then it would be as simple as using a conditional:
<% if thing.something.empty? %>
<%= f.select ... %>
<% else %>
<%= thing.something %>
<% end %>
If my assumptions were incorrect, please reply and I'll revise.

User HTML Helper to populate a DropDown list

I'm trying to populate a dropdown list with a list returned by a query to my object's facade. I've seen a couple examples here but nothing close enough to my use case. Seems like:
<%= Html.DropDownList("User.Affiliate", UserFacade.Instance.SelectAffiliates())%>
should work but doesn't.
Try using
<%= Html.DropDownList("User.Affiliate",
new SelectList(UserFacade.Instance.SelectAffiliates()))%>

Resources