Show button to another page using cocoon gem - ruby-on-rails

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>

Related

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'])%>

Select checkbox pass array in ruby on rails using kaminari pagination

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.

Dynamic checkbox with Ajax in Rails

I have a Form with some checkboxes loaded from the Database, and an option to add other items manually to the items table. This is done by Ajax in the code below...
item_controller.rb
def manual_item_add
#manual_item = Item.find(params[:id_item].to_i)
respond_to do |format|
format.js
end
end
manual_item_add.js.erb
$("#items_table").append("<%= escape_javascript(render 'manual_item_add', :manual_item => #manual_item) %>")
_manual_item_add.html.erb
...
<td><%= check_box_tag("manual_items[]", item.id, true) %></td>
...
edit_items.html.erb
<%= form_tag( {controller:"item", action:"edit_items"}) do %>
<p align="center">
<%= select_date(#account.start_date, prefix: 'start_date') %>
to
<%= select_date(#account.end_date, prefix: 'end_date') %>
</p>
<%= hidden_field_tag 'id_account', #account.id %>
<table id="items_table" class="subtable" align="center" width="55%">
....
<tr>
<th colspan="6">Items added manually</th>
</tr>
<tr>
<th># ID</th>
<th>Type</th>
<th>Description</th>
<th>Ammount</th>
<th>Date</th>
<th>Include in the account</th>
</tr>
</table>
<p align="center"><%= submit_tag("Save", class: "btn") %></p>
<% end %>
<%= form_tag( {controller:"item", action:"manual_item_add"}, method:"get", remote: true) do %>
<h4 align="center">Add item manually</h4>
<p align="center">Item ID:
<%= text_field_tag "item_id", nil , size:5, maxlength:5 %>
<%= submit_tag("Add Item", class: "light_btn") %>
</p>
<% end %>
So... the problem here is that though I see the new checkboxes i am adding to the table (they are being created normally), the "manual_items[]" array is not being passed to the controller when I submit the resulting form (by the way, the "items_table" is inside the form definition).
Does anybody know what I am doing wrong? Sorry for the newbie question, I'm starting to work with Ruby + Rails.
Unfortunately, I don't have a definitive answer for this problem. The only working solution I've tried was to use JQuery to force parameters to be part of the form:
$(document).ready(function() {
$("#edit_items_form").submit(function(event) {
$(this).append($("input[name='manual_items[]']:checked"));
$(this).submit();
});
});
I am definitely not comfortable to this solution, I'd like to know why these Ajax checkboxes are not automatically recognized as being part the form.

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.

How to Handle 2 tables with one form_tag? Rails 3

Beneficiaries
id (PK)
name
age
income
Beneficiary_loans
id (PK)
beneficiary_id (FK)
amount
rate
period
What I'm doing is, selecting a list of beneficiary from [Beneficiaries] table and displaying as follows
<%= form_tag({:action => 'update_survey_list_status', :status=>4}) do %>
<table width="100%">
<tr>
<th>Beneficiary Details</th>
<th>Amount</th>
<th>Rate</th>
<th>Period</th><th>
<input type='checkbox' name='checkall' onclick='checkedAll();'>
</th>
</tr>
<% #publishedlist.each do |b| %>
<tr>
<td><%= b.firstname %></td>
<%= fields_for :beneficiaryloan do |bloan| %>
<td> <%= bloan.text_field :amount%></td>
<td> <%= bloan.text_field :rate%></td>
<td> <%= bloan.text_field :period%></td>
<% end %>
<td><%= check_box_tag "benificiary_ids[]",b.id, :name => "benificiary_ids[]"%> </td>
</tr>
<% end %>
</table> <%= submit_tag "Approve", :class=>'form_buttons' %>
<% end %>
In controller,
#beneficiaries=Beneficiary.find(:all, :conditions => ["id IN (?)", params[:benificiary_ids]])
#beneficiaries.each do |b|
#beneficiaryloan = Beneficiaryloan.new(params[:beneficiaryloan])
#beneficiaryloan.beneficiary_id=b.id
#beneficiaryloan.hfi_id=session[:id].to_s
#beneficiaryloan.status_id=params[:status]
#beneficiaryloan.save
end
What I'm not getting is
params[:beneficiaryloan]
Values are coming as NULL
Am I missing something here in this form?
Check the following,
With the help of any browser tool(eg:firebug), make sure that the form has been rendered properly. Sometimes due to some misplaced tags(generally happens with table structure) the form does not renders properly.
Try to remove the table structure and see if the form works.
Make sure your association is fine, i guess it should be Beneficiary has many Beneficiaryloan
If none of the above helps, please post the request parameters over here.

Resources