In the Rails view users/index.html.erb
I iterated over each users to list them all.
Want sequence no. in front of the user list. I have used each_with_index to achieve this.
I want to know if rails provides any better alternate to this. I am using rails 4.1
<% #users.each_with_index do |user,count| %>
<tr>
<td><%= count+1 -%></td>
<td><%= user.name %></td>
Related
I'm looking for the best way to implement click (arrow icons) sort per column in an HTML table (not the database table). Is this best done with the boostrap-sortable plugin or best to try and implement an action in Rails to sort (controller method). It currently loads A-Z by name, as implemented by the query.
I have tried referring to: https://mdbootstrap.com/docs/jquery/tables/sort/ but received console errors that DataTables(); could not be found.
#users = User.all
<thead>
<th></th>
<th>Name</th>
<th>State</th>
<th>Phone</th>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<td></td>
<td><%= user.full_name %></td>
<td><%= user.state %></td>
<td><%= number_to_phone(user.phone_number, area_code: true) %></td>
</tr>
<% end %>
</tbody>
</table>
Javascript console: "DataTable();" cannot be found.
IMHO I think is better to implement the sort option in the backend, because your list could have 10000 records and you are showing for example 10 per page. If you are delegating the process in the client-side I think this is not a good strategy.
Maybe you can implement a scope in order to resolve the problem. The index action of the controller must have params in the query string to apply for the order.
example:
myurl?order=asc&by=name
Then in the index action you
I am building a rails dictionary application. I have defined and created my whole dictionary in a simple table that draws from a .txt document via a buffer where the view looks like this:
<% #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 %>
Controller:
def index
#words = Word.all
end
How do I go about adding an alphabetical index at the top of the page that will display only the words for that letter? I am new to rails and ruby so any help is appreciated.
The alphabetical_paginate gem helps you implement this.
If you wish to not use a gem, this answer has some tips that you can borrow.
I'm still new to Rails, and can't seem to wrap my head around this.
I have table showing of several Products with several attributes in form columns. In my index view I'm showing a shortened table, with just some of the columns. In my products#show I'm telling the full story, but obviously only for the selected Product. What I want to do, is letting users select a number of Products in products#index, and then store the selected IDs in a hash, sending them to another view, which I want the IDs to be presented in a full scale table, telling the whole story, of several products at once. Basically this is for comparing Products.
I'm not sure how to tackle this. As I'm already using DataTables, using javascript would make sense, but I'm unsure on how. And ideas? I've tried several ideas, but none of them got me anywhere.
products#index
<table id="products"cellpadding="0" cellspacing="0" border="0" class="table.responsive">
<thead>
<tr>
<th></th>
<th>Product Name</th>
<th>TG</th>
<th>TD</th>
<th>DK</th>
<th>DF</th>
<th>T260</th>
<th>T288</th>
<th>CTI</th>
<th>Strength</th>
<th>Rigidity</th>
<th>Standards</th>
<th>Manufactorer</th>
</tr>
</thead>
<tbody>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag "product_ids[]", product.id %></td>
<td><%= link_to product.name, product %></td>
<td><%= product.TG %></td>
<td><%= product.TD %></td>
<td><%= product.DK %></td>
<td><%= product.DF %></td>
<td><%= product.T260 %></td>
<td><%= product.T288 %></td>
<td><%= product.CTI %></td>
<td><%= product.ElectricStrength %></td>
<td><%= product.rigidity %></td>
<td><% product.standards.each do |standard| %>
<%= link_to standard.name, standard %>,
<% end %> </td>
<td><%= product.producer %></td>
<% end %>
</tr>
</tbody>
</table>
If anyone could spare some time to point me in the right direction, that would be wonderful!
This shouldn't be too tough to tackle, I'll walk you through the basic steps that I would take.
My basic approach would be to take a form of said checkboxes and submit it to an action designed to handle them in the way described, like so:
Create a new route to use for all of this. Take a look at 2.10.2 Adding Collection Routes in the docs (assuming you are using resources). Maybe call it something like "compare".
Use a form not backed by a model to hold your checkboxes and labels (1 of each for each product) - it should post to your new route.
In your controller, add the action for your route. This action should fetch the ids passed in by the form from the params and load the relevant products (remember that find can take an array of ids, so it should be pretty easy).
Finally, add a view for your new action, with the relevant logic to present your comparison.
You can easily come in an backfill this with javascript, but it is not required. Honestly, it would be more useful for adding a little extra panache, like disabling the button and showing a spinner, or something, than a full-on javascript submission, which doesn't really buy you anything in this particular case.
I'm just trying to resolve a dilemma of Partials and Helpers in RubyOnRails 3.2.8. I'm new and yes, i tried to find, but every answer is not clear or old more than 2 years. So what's the deal?
I wonder what is best to use for this snippet. Helper or partial ?
<td><%= User.where("id = ?", job.created_by_id).first.email %></td>
<td><%= job.document.to_s.from(53) %></td>
<td><%= job.translation_length %></td>
<td><%= job.translation_language %></td>
<td><%= job.cost %></td>
<td><%= job.translation_type %></td>
<td><%= job.comments %></td>
<td>
<% if job.as_soon_as_possible.to_i.equal?(0) %>
<%= job.due_to_date %>
<% else %>
ASAP
<% end %>
</td>
<td><%= job.status %></td>
I have used both, both work fine, but i'm not sure, if it's good hold it in partial. What about performance between rendering partial and calling helper method ? Read something about it 2 yrs old, and helpers seemed to be way faster. Is there any improvement of rendering partials?
Second, I read about using partials and helpers, but there are many opinions of developers. Am I right : Helpers are using with snippets containing more ruby code with html and Partials with html and small amount of ruby code. (According to this idea, my code above should be placed to helper)?
I think, that helper methods are for specific proposes, they provide uniform and centralized generation of routes, and allows to avoid building some repeating html structures (in conjunction or not with model objects). And a partial is a part of the page, some semantic independent block.
Consider your code.Now you probably listing jobs ,suppose these piece of code repeat more than one places you create partial,like adding/updating record has same fields so create form partial
Now,below code will return job due date or ASAP
<td>
<% if job.as_soon_as_possible.to_i.equal?(0) %>
<%= job.due_to_date %>
<% else %>
ASAP
<% end %>
</td>
So creating helper which will return due date or ASAP depending upon need.
There are many more difference between partial and helper.May be if i found some useful articles i will share with you.
Right now, if I go to the index action of a model that I have, I don't show the basic table of data that rails generates for me if there are no existing records in the database. I do something like:
<% if #my_records.count > 0 %>
<table>
<tr>
<th>...</th>
<th>...</th>
<th>...</th>
<th>etc</th>
</tr>
<% #my_records.each do |my_record| %>
<tr>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
</tr>
<% end %>
</table>
<% end %>
This works locally. However, when I push my app to heroku, this causes a 500 error and the log says:
ActionView::TemplateError (undefined method 'count' for []:Array) on line ...
So I change it to .length and it works fine. Can anyone tell me why that is? Someone has told me that these were redundant and rails got rid of .count but my understanding was that .length is an Array function that tells you how many items are in the Array and .count was an ActiveRecord method for determining how many items in the array were actual records in the database.
Can anyone shed light on this for me?
That's ruby issue, not rails. Locally you probably have 1.8.7, and heroku has 1.8.6. The Enumerable#count method was introduced in 1.8.7: compare http://ruby-doc.org/core-1.8.6/classes/Enumerable.html and http://ruby-doc.org/core-1.8.7/classes/Enumerable.html.