Empty list of attributes to change on Ruby on Rails - ruby-on-rails

I am trying to submit a grid form like the below one,
show.html.erb
<%= form_for :datadef, url: update_all_vmodule_path do |fdf|%>
<table id="csvfiles" class="display" width="100%">
<thead>
<tr>
<th width="10%">Column No</th>
<th width="20%">Column name</th>
<th width="20%">Data type</th>
<th width="15%">Column format</th>
<th width="10%">Length</th>
<th width="25%">Comments</th>
</tr>
</thead>
<tbody>
<% #datadefrecords.each do |ddre| %>
<%= fields_for "datadef_records[]", ddre do |dr_fld| %>
<tr>
<td><%= ddre.column_no %></td>
<td><%= ddre.column_name %></td>
<td><%= dr_fld.collection_select :column_datatype, Datadef.select(:column_datatype).uniq, :column_datatype, :column_datatype, {:prompt => true} %> </td>
<td><%= dr_fld.text_field :column_format %></td>
<td><%= dr_fld.text_field :column_length %></td>
<td><%= dr_fld.text_field :comments %></td>
</tr>
<% end %>
<% end %>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td colspan="2"><%= fdf.submit 'Save' %> <%= link_to("<button>Cancel</button>".html_safe, cancelmodal_vmodule_path ) %></td>
</tr>
</tbody>
</table>
<% end %>
<% end %>
This the method i am calling it to submit
def update_all
session[:datadef] = nil
#updatedatadef = Datadef.all.where(:id => params[:datadef_records]).update_all(update_all_params)
redirect_to migproject_path(params[:vmodule][:migproject_id])
end
This is the strong parameters
def update_all_params
params.require(:datadef_records).permit( :column_datatype, :column_format, :column_length, :comments)
end
This is how i get the parameters from form
{"utf8"=>"✓",
"authenticity_token"=>"QrX8JYYYQOlfwKgAABJd0+A7VpugS2Y6n8doDsuKqeM=",
"datadef_records"=>{"12"=>{"column_datatype"=>"varchar",
"column_format"=>"2",
"column_length"=>"2",
"comments"=>"2"},
"13"=>{"column_datatype"=>"varchar",
"column_format"=>"2",
"column_length"=>"2",
"comments"=>"2"}},
"commit"=>"Save",
"id"=>"2"}
But i get this "Empty list of attributes to change" error which is not allowing me to write it in table. And i am not able to identify what could be the error.
Thanks in advance.

I think the problem is to do with your use of update_all, although I'm not 100% sure as to the syntax of the problem.
From what I understand, update_all needs a hash of actual params to populate your data; even then, it will only update what you pass to it (kind of like update_attributes).
Here's a good reference:
data = params[:datadef_records]
#updatedatadef = Datadef.update(data.keys, data.values)
This negates the use of your strong_params, which I'd recommend using. I'd have to spend some time thinking about getting it to work.

Related

Rails mapping by max size and loop

In my rails application I'm looking to display in a table some results mapping by max size.
Controller
#max_length = [#result.generals, #result.measurements.in_groups_of(3)].map(&:size).max
The problem i'm facing is my #result.measurement has 3 dates for each measurement in which I want to display across the row.
This is what I'm looking to achieve:
But with the code below, this is my result - It stops at the first date:
How can I go about looping the #result.measurements?
<table>
<tr>
<th colspan="2">General</th>
<th colspan="6">Measurements</th>
</tr>
<tr>
<td>Number</td>
<td>Town</td>
<td>Date</td>
<td>Length</td>
<td>Date</td>
<td>Length</td>
<td>Date</td>
<td>Length</td>
</tr>
<% #max_length.times do |data| %>
<tr>
<td><%= #result.generals.[data].try(:number) %></td>
<td><%= #result.generals.[data].try(:town) %></td>
<td><%= #result.measurements.[data].try(:date) %></td>
<td><%= #result.measurements.[data].try(:length) %></td>
</tr>
<% end %>
</table>
Your code is doing exactly what you are asking it to do: to display a single td for the measurements. In order to have three lots of the columns, you need to loop over the data eg something like:
<table>
<tr>
<th colspan="2">General</th>
<th colspan="6">Measurements</th>
</tr>
<tr>
<td>Number</td>
<td>Town</td>
<td>Date</td>
<td>Length</td>
<td>Date</td>
<td>Length</td>
<td>Date</td>
<td>Length</td>
</tr>
<% #result.measurements.in_groups_of(3).each do |general, measurements| %>
<tr>
<td><%= general.try(:number) %></td>
<td><%= general.try(:town) %></td>
<% measurements.each do |m|
<td><%= m.try(:date) %></td>
<td><%= m.try(:length) %></td>
<% end %>
</tr>
<% end %>
</table>
Note: of course I have not bug-tested this - it will probably not work exactly as is - because I do not have a copy of your data (and you have not provided an example) so you'll need to adapt what I've written to what you have so that it works...
At present I don't know how your general relates to your measurements.
can you provide some example data that you would expect to be returned by the fetching-code and I can adapt this example to fit.

Ruby on rails: filtering search box

I am creating a rails application that replicates an online dictionary. I have already implemented the alphabetical_paginate gem but I want to add a search bar that can filter through the words as you type for the word you're looking for. Is there a gem that could do this or is there a straight forward way of filtering with a search bar?
My controller looks like this:
def index
#words, #alphaParams = Word.all.alpha_paginate(params[:letter], {:default_field => "all"}){|word| word.word}
end
View:
<%= alphabetical_paginate #alphaParams %>
<table>
<thead>
<tr>
<th>Word</th>
<th>Wordtype</th>
<th>Description</th>
<th colspan="3"></th>
</tr>
</thead>
<div id="pagination_table">
<tbody>
<% #words.each do |word| %>
<tr>
<td><%= word.word %></td>
<td><%= word.wordtype %></td>
<td><%= word.description %></td>
<td><%= link_to 'Show', word %></td>
</tr>
<% end %>
</tbody>
</div>
</table>
Datatables is what you are looking for.
Here are the links to get started:
http://datatables.net/
https://github.com/rweng/jquery-datatables-rails

Rails convention for case statement in view

I am new to ruby and rails so thought I'd ask a question on conventions.
I have a view which produces a list of items in a table and I was asked to make a change and in doing so have added a case statement to the view which I don't think is the correct way of doing things so thought that I would double check.
The change I made was just to add a class to the tr depending on the value of the last table column.
list.rhtml
<table width="100%">
<tr>
<th style="width: 80px;">ID #</th>
<th>Organisation</th>
<th>Product</th>
<th>Carrier</th>
<th>Carrier Ref</th>
<th>Post Code</th>
<th>Status</th>
</tr>
<%= render :partial => 'circuit/list_item', :collection => #circuits %>
</table>
list_item.rhtml
<%
# code I have added
#tr_class = ''
case list_item.status
when 'Handover'
#tr_class = ''
when 'Unprocessed'
#tr_class = 'high_priority'
when 'Ceased'
#tr_class = 'low_priority'
else
#tr_class = ''
end
# end of newly added code
%>
<!-- the class part is new aswell -->
<tr class="<%= #tr_class %>">
<td><a href='/circuit/update/<%= list_item.id %>'><%= list_item.id_padded %></a></td>
<td><%= list_item.organisation.name if list_item.has_organisation? %></td>
<td><%= list_item.product_name %></td>
<td><%= list_item.carrier.name %></td>
<td><%= list_item.carrier_reference %></td>
<td><%= list_item.b_end_postcode %></td>
<td><%= list_item.status %></td>
</tr>
Is there a Rails pattern or convention that can get the case statement out of this view?
If understand properly your question, I think you should put the case statement inside a helper function:
app/helpers/list_helper.rb
module ListHelper
def tr_class_for_status(status)
case status
when 'Unprocessed'
'high_priority'
when 'Ceased'
'low_priority'
else
''
end
end
end
_list_item.rhtml
<tr class="<%= tr_class_for_status(list_item.status) %>">

HTML Table issue grabbing data from two database tables

Iam having an issue getting data from two different database tables to display properly with in an HTML table. I'm sure I am just overlooking something and this is pretty easy, but for whatever reason I cannot get quantity to display, or the <td class="sub-total"> to display either.
Demands (you can think of these as orders) and items are connected through a table called demand_items. The demand_items table also has the quantity of the item ordered. The page will display but the quantity and the subtotal will not render.
Here is the html:
<h3>Order for <%= #demand.customer.name %></h3>
<h4>Ordered for: <%= #demand.date %></h4>
<h4>Add Items</h4>
<%= render 'demands/item_form' %>
<table class="customer-table">
<tr>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub Total</th>
</tr>
<% #demand.items.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<% #demand.demand_items do |quantity| %>
<td><%= quantity.quantity %></td>
<td class="sub-total"><%= (item.price) * (quantity.quantity) %></td>
<% end %>
<% end %>
</tr>
</table>
#demand.demand_items.each do |quantity|..........instead of #demand.demand_items do |quantity|......

Rails Links with Model Object Path

I have the following table, and I want the name of my "restaurant" to be clickable and link to the page of that restaurant.
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Adress</th>
<th>City</th>
<th>No. of Recipies</th>
</tr>
<% #city.restaurants.each do |rest| %>
<tr>
<td><%= rest.name %></td>
<td><%= rest.adress %></td>
<td><%= rest.city.name %></td>
<td>5</td>
</tr>
<% end %>
How am I going to add something like rest_path to:
Link text
You could try this :
<%= link_to rest.name, rest %>
If you have defined a resource restaurant in your config/routes.rb, it will target the url given by the helper restaurant_path(rest) (ie /restaurants/id_of_restaurant).
As the restaurant is a child of a city you might want to have an url like /cities/id_of_city/restaurants/id_of_restaurant so you could try the following :
<%= link_to rest.name, city_restaurant_path(#city, rest) %>
Be sure to have this in your config/routes.rb in order to generate the corresponding helpers.
resources :cities do
resources :restaurants
end
Then you will be able to see all the available helpers for your routes using the command 'rake routes' in the terminal.
Okay so I did something like this
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Adress</th>
<th>City</th>
<th>No. of Recipies</th>
</tr>
<% #city.restaurants.each do |rest| %>
<tr>
<td><%= link_to rest.name, rest %></td>
<td><%= rest.adress %></td>
<td><%= rest.city.name %></td>
<td>5</td>
</tr>
<% end %>
</table>
but now the link is very ugly when I hover on it? How do I get rid of that?

Resources