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}%>
Related
(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.
I have a page that is a report from a database and I'm working on modifying how the filtering works. The intention is to allow the user to select possible values form a list that will be used to filter the resulting report. There are too many values to do this with checkboxes. I'm defining a multiple selection list box with this:
<g:select name="country" from="${countryDataList.KOUNTRY}" value="${params.country}" multiple="true" />
countryDataList is a List<> of objects with a name and a value which I create in the controller. I'm able to get the selected counties and process them without an issue.
But when the page returns from the controller with the filtered report, only the first selection in the list is selected. It doesn't re-select all of the items that the user selected. I am passing the params.country object back from the controller as
country:params.country
I saw some posts about this not working, but they are all from several years ago. Am I missing a vital step?
Ahh sorry, I was reading it on the phone initially and missed the point.
So what you want is a way of sending a multiple select box to a confirmation page. If I understand correctly?
Anyways how many objects in the select are we talking massive or a dozen couple of dozen or so ?
What I did was use check boxes and did a confirmation which shows the selection ticked in check boxes.. So this is the confirmation page that loads in https://github.com/vahidhedayati/mailinglist/blob/master/grails-app/views/mailingListEmail/confirmcontact.gsp
this page which is where multiple attachments selected from the schedule re-appear...
https://github.com/vahidhedayati/mailinglist/blob/master/grails-app/views/mailingListAttachments/_mailerAttachmentsDisplay.gsp.
Please note advice below is all conceptual stuff and there may be easier ways than this
Other than that You could create a taglib call on the confirmation page https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/taglib/ajaxdependancyselection/AutoCompleteTagLib.groovy#L55 which takes in your arrayList you could probably convert it to JSON pass it into the javascript that you load in within the taglib (on mine further down it loads this page in)
https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/views/autoComplete/_selectJs1.gsp#L23
and look to reselect them using javascript... as I say I haven't tested the last bit, the first bit i.e. checkbox works it is/has been in use.
Years later from you I just had the same problem. What I figured out is: it happens when params.country is an array instead of a Collection (i.e. an ArrayList).
A workaround for this if you want to stick to the array type is at the value attribute of the tag doing this: params.country?.findAll().
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!
I'm new at ruby on rails, and I wanted to accomplish the following:
I've got a list with check boxes(one per row) and I'd like to have a link button(not a common button or submit) so when I click it I call an action from a controller.
My questions are:
How can I call a controller action with the link_to?
How do I get the checkboxes values(which of them are checked) and fields' values so I can know to whom apply the action? I'm used to work with C#, and there the lists have an ItemDataBound method in where you can get easily every row lets say, but I can't figure anything similar here.
If it's not clear enough, I'm going to put an example:
Let's say I've got the following "screen":
Delete(link)
Article ID | Article Name
chkbox 1111 t-shirt
chkbox 2222 pants
chkbox 3333 boots
Now, let's say I'd like to delete the pants and boots. So I'll check their check boxes and then I'll press Delete. Now, I'd like to have at my Articles controller, at the method delete_article(for example) and then get the id and name for those checked articles, and delete them.
Thanks,
Brian
I would wrap the checkboxes in a form and then submit this form using the link (either with javascript or change the link to a form button and style as a link).
Rails assumes a RESTful approach out of the box, so a straight link will always hit a GET accessible action on your controller (generally index or show). GET actions should always be idempotent.
You can use link_to the standard way, check out the rails documentation on 'link_to'.
The values from checkboxes can be get from the params hash.
Just look out for the documentation.
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()))%>