Select checkbox pass array in ruby on rails using kaminari pagination - ruby-on-rails

I have my view where I am showing all the records and I need to add a checkbox for multiple selection, I am doing it in the following way:
<%= form_tag("/user_selection", method: 'get', remote: true, class: "form-horizontal" ) do %><!--responde al index-->
<div class="rwd">
<table class="table table-striped table-bordered table-condensed table-hover">
<thead>
<tr>
<th>name</th>
<th>last name</th>
</tr>
</thead>
<tbody id="container_users">
<%= render #users %>
</tbody>
</table>
<%= paginate #users, :param_name => 'users' %>
</div>
<%= submit_tag "send", data: { disable_with: 'sending...' } %>
<% end %>
partial user
<tr id="user_<%= user.Id %>">
<td><%=user.try(:name)%></td>
<td><%=user.try(:lastname)%></td>
<td><%= check_box_tag "items[]", user.Id %></td>
</tr>
This way I can select multiple records and send the ids to the controller, the problem comes when I select the records and I go to the next page using the page of kaminari, when passing from page the records of the previous page are no longer selected, like could you solve it?

1). Save all selected users to form's hidden field selected_user_ids. Kaminari should send remote request and update only user's table but not whole page. In this case selected_user_ids will keep all seleted users.
Second way:
Save all selected users to localStorage. On form submit extract all users ids from localStorage and save them when to hidden field and send form.

Related

Show button to another page using cocoon gem

I am using cocoon on one of forms and i want to add a show button to each row which will direct users to another model's show page. However, while everything is working smoothly i can not make this button to show up.
So this my current view:
<div class = "nested-fields">
<div class="table-responsive">
<table class= "table table-hover">
<tr>
<th> Product ID </th>
<th> Button </th>
</tr>
<tr>
<td> <%=f.object.product_id%> </td>
<td> <%= link_to 'Show', product_path(f.object.product_id), class: "btn btn-outline-success", target: :_blank %> </td>
</tr>
</table>
</div>
</div>
When i try to open this page, i am getting this error:
No route matches {:action=>"show", :controller=>"products", :id=>nil}, missing required keys: [:id]
However, i know that product_id is not nill because, on the table i can print the product_id for each line. And, my product routes are definitely fine, i can already use them including with show action. And i know that product_id has a matching id in products table.
Also, if try to go to products' index page by using:
<%= link_to 'Show', products_path(f.object.product_id), class: "btn btn-outline-success", target: :_blank %>
the following url is going to be generated:
http://localhost:3000/products.97
I just cannot understand why it cannot get the id when i use it product_path.
Any help will be regarded.
Thanks.
From the conversation/comments I can only conclude one of the items product_id is actually really nil. Easy way to make sure your table/page still renders is write your view as follows
<td>
<% if f.object.product_id.present? %>
<%= link_to 'Show', product_path(id: f.object.product_id), class: "btn btn-outline-success", target: :_blank %>
<% end %>
</td>

Pass html table td value to rails controller

In my html I'm displaying some data from two tables. I provided an edit button for each row of the table. When I click on it, it has to check if the name is existing in table1 or table2 and take all the details of that particular name.
HTML:
<div class="col-md-12">
<h5 style="text-align: center;">List of SNMP OIDs</h5>
<table id="myPersonTable" class="table table-striped" >
<thead>
<tr>
<th>Person Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody id="table_body">
<% #all_persons.each do |person|%>
<tr>
<td>
<%= person['name'] %>
</td>
<td>
<%= link_to '<button type="button" class="btn btn-info">Edit</button>'.html_safe, edit_oid_path(person['id'])%>
</td>
<td>
<%= form_tag(contoller: "configuration", action: "delete_person") do%>
<%= hidden_field_tag(:person_id, person['id'])%>
<%=submit_tag "Delete", class: "btn btn-danger", data: {confirm: "Are you sure?"}%>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
Rails Controller:
def edit
person_f = Person.find_by(name: params[:name])
person_s= HardPerson.find_by(name: params[:name])
if person_f.present?
#person = Oid.find(params[:id])
elsif person_s.present?
#oid = HardPerson.find(params[:id])
end
end
Here is a problem: I click on edit button for a person name from person2
table having id=1. This id exists in person1 and person2 tables both. Instead of taking details from person2 it is checking that id in person1 table and it is getting values for id=1 person details from person1 table
Here in controller params[:name] is getting null value. Help me to get params[:name] in my rails controller
To get params[:name] in your controller use the :param option in your routes to override the default resource identifier :id
resources :people, param: :name
This will allow you to use:
Person.find_by(name: params[:name])
Keep in mind that you might need to to override ActiveRecord::Base#to_param of a related model to construct a URL.
See 4.10 Overriding Named Route Parameters for more information.
It's a bit hard to understand what is your issue, but I'll try. Am I right that you want to see value from <%= person['name'] %> as a params[:name] inside edit action? Just pass it as additional argument to path helper:
<%= link_to '<button type="button" class="btn btn-info">Edit</button>'.html_safe,
edit_oid_path(person['id'], name: person['name'])%>

Filtering table with dropdown menu - extra unwanted code displayed on the page

Overflowers,
I was trying to build a table which would be filtered by dropdown menu selection. The whole proccess works in this way:
Index.html.erb
render "selection"
This partial gives us a dropdown menu with selections.
_selection.html.erb
<%= select_tag :area_selection,
options_from_collection_for_select(Maps.all, :id, :location),
:remote => 'true',
:onchange => 'location_request(this.value);' %>
Selection from dropdown menu is being sent to a JS (which is located on index.html.erb)
location_request javascript
function location_request(area_selection){
jQuery.ajax({
url: "/location_filter",
type: "GET",
data: { "requested_area" : area_selection },
dataType: "script",
success: function(data){
// You are the winner :)
},
error: function(data){
console.log(data);
}
})
}
From here, a variable is being sent to a controller
controller
def location_filter
#filtered_locations = Post.where("location" => params[:requested_area])
respond_to do |format|
format.js
end
end
In this stage, the location filter method returns a new "table" with filtered elements to a location_filter.js.erb
location_filter.js.erb
$('#info').html("<%= escape_javascript(render partial: 'filtered_table')%>");
Here, a table is being imported to a #info div on a website via table partial
_filtered_table.html.erb
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Location</th>
</tr>
</thead>
<tbody>
<%= #filtered_locations.each do |filtered| %>
<tr>
<td><%= filtered.location %></td>
</tr>
<% end %>
</table>
And this is the final format of the table which works perfectly fine as for a beginner.
The issue I face is that I get an extra table in text format which is being added on the same #info div
So it's the main issue - there is unwanted data displayed on the page in yellow. It needs to be removed somehow.
Question:
As far as I understand, the extra outcome is being made in controller (?) and then added to a front view.
What causes this duplicate of a table and how to avoid having it? :) Lame questionz
This isn't a controller error, is a error at your view. You're using <%= %> for your loop, and the <%= is used when you want to print something in your view, which, in your case, is the collection of objects that came from your controller. Try this:
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Location</th>
</tr>
</thead>
<tbody>
<% #filtered_locations.each do |filtered| %>
<tr>
<td><%= filtered.location %></td>
</tr>
<% end %>
</tbody>
</table>
Note that I removed the equal sign from your loop.

rails 5 form_tag with original DB value and after new selection in form, both records must be updated

I have a form_tag with a radio_button_tag and it populates with data from DB. It must simply be directed to a customised update action(update_multiple) where a boolean column is updated for all those 2 records which have been changed in the form.
For e.g. say when initially the form was populated from DB with record 1's radio button selected and now user changed his selection to record 3, then at Submit of form tag the update of both records must occur but the problem is the code at submit, only collects id of record which is now selected in that group.How do I get id of that record also which was unselected so that I can update_all for both of them at one go?
And if Submit cannot handle this action, then is there a way in controller or form to persist the id of the initial selected record before populating the form? As you see, I've tried with collecting an array of ids[] with radio_button_tag.
TIA for your help.
Here's the form code:
<%= form_tag update_multiple_user_cv_attachments_path, method: :put, action: :update_multiple do %>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th> Select a CV </th>
<th> Resume Name </th>
</tr>
</thead>
<tbody>
<% #cv_attachments.each do |cv_attachment| %>
<%= hidden_field_tag cv_attachment.main, :value => params[:main] %>
<tr>
<td><%= radio_button_tag "cv_attachment_ids[]", cv_attachment.id, cv_attachment.main %> </td>
<td><%= cv_attachment.attachment.file.basename %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= submit_tag "Select Main", :class =>'button' %>
<% end %>
Here's the controller update_multiple code.
def update_multiple
CvAttachment.update_all(["updated_at=?", Time.now], :id => params[:cv_attachment_ids])
end
I can think of 2 ways to achieve your objective.
update the boolean for all the attachments which belong to the user to false and then update the one which has been selected to true
include a hidden field in the form and set it to the id that is already true. Then in the controller action, update the one that is selected to true and the one in the hidden field to false. This is probably a better option and you'll probably want to wrap the d/b updates in a transaction.
<tbody>
<% #cv_attachments.each do |cv_attachment| %>
<% if cv_attachment.main %>
<%= hidden_field_tag "ex_main_cv", cv_attachment.id %>
<% end %>
<tr>
<td><%= radio_button_tag "main_cv", cv_attachment.id, cv_attachment.main %> </td>
<td><%= cv_attachment.attachment.file.basename %></td>
</tr>
<% end %>
</tbody>
controller
def update_main_attachment // probably a better name for this method
if params["ex_main_cv"] != params["main_cv"]
Attachment.transaction do
deselected_attachment = Attachment.find(params["ex_main_cv"]
deselected_attachment.update_attribute(:main, false)
selected_attachment = Attachment.find(params["main_cv"]
selected_attachment.update_attribute(:main, true)
end
end
end
Many thanks #margo. Here' how I resolved it partly your way of using hidden_field. But for now keeping this thread open as I'm making 2 DB updates for toggle of same column.
<tbody>
<% #cv_attachments.each do |cv_attachment| %>
<% if cv_attachment.main %>
<%= hidden_field_tag "ex_main", cv_attachment.id %>
<% end %>
<tr>
<td><%= radio_button_tag "new_main", cv_attachment.id, cv_attachment.main, :id => "#{cv_attachment.id}"%> </td>
<td><%= cv_attachment.attachment.file.basename %></td>
</tr>
<% end %>
</tbody>
and in controller:
def update_main
if request.put?
if params["ex_main"] != params["new_main"]
CvAttachment.find(params[:ex_main]).toggle!(:main)
CvAttachment.find(params[:new_main]).toggle!(:main)
end
end

rails form_tag and radio button that calls the destroy method

I am creating an admin page for my app and it simply displays everything in my database. I want to have one of the table header's be delete, and the table data be a radio button that when clicked and hit enter, deletes that column from my table (and database).
I have the DELETE route created from calling resources :urls in routes.rb:
resources :urls
# DELETE /urls/:id(.:format) urls#destroy
I have the Destroy Method in my url Controller:
def destroy
id = params[:id]
url = Url.find(id)
url.destroy
end
And in my admin.html.erb I have the following code:
<div class="webpage-name"> Admin Page </div>
<table class="table table-hover table-striped">
<thead>
<tr>
<th> id </th>
<th> link </th>
<th> random_string </th>
<th> clicks </th>
<th> delete </th>
</tr>
</thead>
<tbody>
<% #urls.each do |url| %>
<tr>
<td><%= url.id %></td>
<td><%= url.link %></td>
<td><%= url.random_string %></td>
<td><%= url.clicks %></td>
<td><%= form_tag(urls_path, method: "delete")%><%= url.radio_button%></td>
</tr>
<% end %>
</tbody>
</table>
</div>
What I am trying to implement is the final <td>. I created a form_tag that sends the information to urls_path with the method: "delete". But now how do I actually make a radio button, checkbox or anything that allows me to select many options and then hit enter and delete them? Also, I'm very new at this so I'm not sure if my form_tag syntax is correct and doing what I believe it to be doing.
When I remove <%= url.radio_button%> from my code, I am able to view the admin page normally and just have an empty delete column.
Try to submit form on radio button click like
<% form_tag(url_path(url), method: "delete") do %>
<%= radio_button_tag :remove, url.id, false, :onchange =>"this.closest('form').submit();" %>
<% end %>
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-radio_button_tag
I am not sure this could work or not just try it.

Resources