How to arrange model items in a grid - model-view-controller

How do I organize my items in a table
so that my row has lets say 4 columns, in my
foreach(var item in Model.Data)
{
<td>item.name</td>
}
How do I have item.name[i], item.name[i+1] so that I can fill out my column for the row?

I'm not clear on exactly what you're asking or what version of MVC or View Engine, but the general pattern of a table displaying a collection of objects is
<table>
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Hair Color</td>
</tr>
</thead>
<tbody>
<% foreach (var item in Model) { %>
<tr >
<td><%= Html.Encode(item.Name) %></td>
<td><%= Html.Encode(item.Description) %></td>
<td><%= Html.Encode(item.HairColor) %></td>
</tr>
<% } %>
</tbody>
</table>

Related

Rails - How to integrate search results into table?

I have this table
t.string "first_name"
t.string "last_name"
t.integer "age"
t.string "email"
I am using some search functionality where I search for a Barber by first_name .
Then I am displaying it and whatever else is similar to the input.
This is my current view (search.html.erb)
<h1>searches</h1>
<%= #barbers.each do |barber|%>
<%= barber.first_name%><br>
<%end%>
Very plain and simple and this is how it looks.
I obviously don't want to keep it like this.
I want it to look like this , the way I have it in my index.html.erb
<table class="table table-dark table-striped">
<thead>
<tr>
<th scope="row">First name</th>
<th scope="row">Last name</th>
<th scope="row">Age</th>
<th scope="row">Email</th>
<th scope="row">User ID</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #barbers.each do |barber| %>
<tr>
<td><%= barber.first_name %></td>
<td><%= barber.last_name %></td>
<td><%= barber.age %></td>
<td><%= barber.email %></td>
<td><%= barber.user_id %></td>
<td><%= link_to 'Show', barber %></td>
<td><%= link_to 'Edit', edit_barber_path(barber) %></td>
<td><%= link_to 'Destroy', barber, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
I am just confused on how to write the code since I am searching for each. #barbers.each do |barber|
Here is what I done...
<h1>searches</h1>
<table class="table table-dark table-striped">
<thead>
<tr>
<th scope="row">First name</th>
<th scope="row">Last name</th>
<th scope="row">Age</th>
<th scope="row">Email</th>
<th scope="row">User ID</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #barbers.each do |barber| %>
<tr>
<td><%= barber.first_name %></td>
<td><%= barber.last_name %></td>
<td><%= barber.age %></td>
<td><%= barber.email %></td>
<td><%= barber.user_id %></td>
</tr>
<% end %>
</tbody>
</table>

how to disable checkbox on condition?

I am trying to create a spreadsheet on index page to display items with checkbox and if validated - the checkbox will be disabled so that people won't mis-ticked it.I was wondering should i do it with controller or the view? thanks : can you help?
In app/views/scooties_coupons/index.html.erb
<table class="table table-hover">
<h1>Scooties Coupons</h1>
<%= form_with(url: validate_coupons_path, method: 'patch') do |f| %>
<table>
<thead>
<tr>
<th>Valid</th>
<th>Coupon</th>
<th>Redeemed</th>
<th>First name</th>
<th>Surname</th>
<th>email</th>
<th>occupation</th>
<th>validation</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #scooties_coupons.each do |scooties_coupon| %>
<tr>
<td>
<%= fields_for('scooties_coupons[]', scooties_coupon) do |cf|
cf.check_box(:validated)
end %>
</td>
<td><%= scooties_coupon.coupon %></td>
<td><%= scooties_coupon.redeemed %></td>
<td><%= scooties_coupon.first_name %></td>
<td><%= scooties_coupon.surname %></td>
<td><%= scooties_coupon.email %></td>
<td><%= scooties_coupon.occupation %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit %>
<% end %>
<br>
</table>
in app/controllers/scotties_coupons_controller.rb:
def set_valid_coupons
to_valid = params[:scooties_coupons].select do |id, attrs|
attrs[:validated] == '1'
end
to_not_valid = params[:scooties_coupons].reject do |id, attrs|
attrs[:validated] == '1'
end
ScootiesCoupon.transaction do
ScootiesCoupon.where(id: to_valid.keys, validated: false).update_all(
validated:true)
ScootiesCoupon.where(id: to_not_valid.keys, validated: true).update_all(
validated:false)
end
redirect_to action: :index, notice: 'Validations updated'
end
you can do something like
<table class="table table-hover">
<h1>Scooties Coupons</h1>
<%= form_with(url: validate_coupons_path, method: 'patch') do |f| %>
<table>
<thead>
<tr>
<th>Valid</th>
<th>Coupon</th>
<th>Redeemed</th>
<th>First name</th>
<th>Surname</th>
<th>email</th>
<th>occupation</th>
<th>validation</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #scooties_coupons.each do |scooties_coupon| %>
<tr>
<td>
<% if true %>
SOMETHING HERE
<% else %>
<%= fields_for('scooties_coupons[]', scooties_coupon) do |cf|
cf.check_box(:validated)
end %>
<% end %>
</td>
<td><%= scooties_coupon.coupon %></td>
<td><%= scooties_coupon.redeemed %></td>
<td><%= scooties_coupon.first_name %></td>
<td><%= scooties_coupon.surname %></td>
<td><%= scooties_coupon.email %></td>
<td><%= scooties_coupon.occupation %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit %>
<% end %>
<br>
</table>

How to use sort_link with model instance method in rails 4 with Ransack Gem?

I have used Ransack Gem,Rails 4.
roles_controllers.rb
def index
#q = Role.ransack(params[:q])
#roles = #q.result(distinct: true)
end
This is instance method in model(role.rb)
def user_count
self.users.count
end
This is my html table header(Roles/index.html.erb)
<table class="table table-bordered table-framed rb-usr-tbl">
<thead>
<tr>
<th><%= sort_link(#q, :name) %></th>
<th>Description</th>
<th><%= sort_link(#q,:role_users_user_count) %></th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody class="role-index">
<% #roles.each do |role| %>
<tr>
<td><%= role.human_role %></td>
<td><%= role.description %></td>
<td><%= role.user_count %></td>
<td>
<%= link_to edit_role_path(role), :remote => true,:title => "Edit Role" do %>
<i class="icon-pencil7 rb-pencil"></i>
<% end %>
</td>
<td>
<%= link_to role, method: :delete,:title => "Delete Role", data: { confirm: 'Are you sure?' } do %>
<i class="icon-trash rb-trash"></i>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
There is relationship between user and role are One role has_many users.
Here i want to sort this count of users in asc and desc order but with this code it does not work.
Help me if you can.
Thanks
Ransack is very smart itself, try to change line
<th><%= sort_link(#q,:role_users_user_count) %></th>
to
<th><%= sort_link(#q, :users_count) %></th>
Explanation: In the variable #q you keep a Ransack query object on the model Role, if you call sort_link on #q it will apply users_count as "join users relation and order by users count".

Rails: call a parent model 's attribute in View?

here i have 2 simple model with one-many relation:
class Category < ActiveRecord::Base
attr_accessible :Name
has_many :items
class Item < ActiveRecord::Base
attr_accessible :Category_id, :Name, :Price, :Description
belongs_to :category
and I've a View to show info about Item like:
<table>
<tr>
<td class="field" style="width: 175px;"><b>Name:</b></td>
<td><%= #item.Name %></td>
</tr>
<tr>
<td class="field"><b>Price:</b></td>
<td><%= #item.Price%></td>
</tr>
<tr>
<td class="field"><b>Category: </b></td>
<td><%= Category.find(#item.Category_id).Name %></td>
</tr>
<tr>
<td class="field"><b>Description: </b></td>
<td><%= #item.Description %></td>
</tr>
</table>
It's work right. But here I've a question: Is there's another ways to call class Category 's attribute. Something like #item.category.Name which I try but didn't work ( undefined method "Name" for nil:NilClass)
Why do you have your attributes in camel case? Downcase them and change this:
<tr>
<td class="field"><b>Category: </b></td>
<td><%= Category.find(#item.Category_id).Name %></td>
</tr>
to this:
<tr>
<td class="field"><b>Category: </b></td>
<td><%= item.category.name %></td>
</tr>
It should work.
Perhaps a nil check would help, as described here.
<tr>
<td class="field"><b>Category: </b></td>
<td><%= #item.category.name unless #item.category.nil? %></td>
</tr>

How to work with string?

I have a string with emails (name, surname, email):
#emails = "Nina Beeu luda#hotmail.com, Vasilina Korute valaj#kos.co.uk, Hikote Ewefs valaj#kos.co.uk,
Egert Erm papa#sasee.ee, Sambuka Ioas valaj#kprivet.com, Vanish Kiki sasa#sas.com, Inoke Xxx saop#hot.ee"
I need to substring from this string: name, surname and email and paste them into table:
<table border=1>
<tr>
<td>
Name
</td>
<td>
Surname
</td>
<td>
Email
</td>
</tr>
</table>
How i can do it?
<table>
<% #emails.split(", ").each do |chunk| %>
<tr>
<% ["Name", "Surname", "Email"].zip(chunk.split(" ")).each do |data| %>
<td><%= data.join(": ")</td>
<% end %>
</tr>
<% end %>
</table>
#emails.split(/,\s+/).each do |details|
name, surname, email = details.split(" ")
# do your html creaty thing here
end
More explicitly, you could do this in erb:
<table border=1>
<% #emails.split(/,\s+/).each do |details| %>
<% name, surname, email = details.split(/\s+/) %>
<tr>
<td><%= name %></td>
<td><%= surname %></td>
<td><%= email %></td>
</tr>
<% end %>
</table>
And a variant in haml:
%table(border=1)
- #emails.split(/,\s+/).each do |details|
%tr
- details.split(/\s+/) do |detail|
%td= detail

Resources