How do I create a dynamic class for a div variable? - html

I have the following code in an ERB file:
<table border="1">
<% #lists.each do |list| %>
<tr class="even">
<td><%= link_to list.title, list %></td>
<td><%= link_to 'Edit', edit_list_path(list) %></td>
<td><%= button_to "Destroy", list_path(list), :method => :delete %></td>
</tr>
<% end %>
</table>
I want to make the <tr class="even" line dynamic. Each tr should get the class either "even" or "odd" depending on a counter variable that gets incremented every time my loop starts over. However, I cannot figure out the best way to implement this.

Use this nice helper :)
<tr class="<%= cycle("even", "odd") %>">

Related

Unexpected list of items on #index

On my index page, I succeeded in listing all of my 'games' in a table, unfortunately, another list, that isn't part of my index.html.erb file code also appears, above my table.
I don't understand how this is possible as my html file doesn't contain any element at the place the list appears on the browser... If someone has an idea that would be very nice !
Here's a photo of what appears on the browser :
browser problem snapshot
Here's my index.html.erb code :
<div class="container full-height">
<div class="abs-center">
<table class="table">
<thead>
<tr>
<td colspan="6">GAMES</td>
</tr>
</thead>
<tbody>
<%= #games.reverse.each do |g| %>
<tr>
<td><%= g.id %></td>
<td><%= g.score_1 %></td>
<td><%= g.score_2 %></td>
<td><%= g.created_at %></td>
<td><%= link_to 'see game', game_path(g) %></td>
<td><%= link_to 'modify', edit_game_path(g) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
My game controller index method :
def index
#games = Game.all
end
Thanks a lot !
Remove = before <%= #games.reverse.each do |g| %>. = renders a result of an expression, in your case, it is each method, that returns the collection.
Must look as this <% #games.reverse.each do |g| %>

Equal distribution for table rows - HTML AND CSS

I just made a table and I have three records that I'm trying to display. It successfully displays the information but it crams it all in one row. When I want it to display over three rows. I'm doing something obvious wrong but I still need some help. Here is my code:
HTML
<table>
<tr>
<th>Name</th>
<th>Manage</th>
</tr>
<tr>
<% #assignments.each do |assignment| %>
<td><%= link_to assignment.name, account_assignment_path(assignment) %></td>
<td><%= link_to "Delete", account_assignment_path(assignment), method: :delete %></td>
<% end %>
</tr>
</table>
Screen Shot:
As you can see they all try to fit in one column. How can I fix this so they spread out?
I'm not familiar with the templating language you're using, but this is likely due to not including the <tr> element in your loop.
<table>
<tr>
<th>Name</th>
<th>Manage</th>
</tr>
<% #assignments.each do |assignment| %>
<tr>
<td><%= link_to assignment.name, account_assignment_path(assignment) %></td>
<td><%= link_to "Delete", account_assignment_path(assignment), method: :delete %></td>
</tr>
<% end %>
</table>

Removing columns from a table

I'm following a tutorial on how to create a Ruby-on-Rails blogging website with comments and tags, and have put my work so far on https://github.com/khpeek/jumpstart-blogger/.
The last part of the tutorial involves allowing authors to create user names and passwords. One of the pages is a default listing of the authors from the "sorcery" gem:
As you can see, there are blank columns with "Password" and "Password confirmation", which I'd like to remove.
The appearance of this page is governed by app/views/authors/index.html.erb, which reads
<p id="notice"><%= notice %></p>
<h1>Listing Authors</h1>
<table>
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Password</th>
<th>Password confirmation</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #authors.each do |author| %>
<tr>
<td><%= author.username %></td>
<td><%= author.email %></td>
<td><%= author.password %></td>
<td><%= author.password_confirmation %></td>
<td><%= link_to 'Show', author %></td>
<td><%= link_to 'Edit', edit_author_path(author) %></td>
<td><%= link_to 'Destroy', author, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Author', new_author_path %>
My thought was to comment out the lines
<th>Password</th>
<th>Password confirmation</th>
and
<td><%= author.password %></td>
<td><%= author.password_confirmation %></td>
However, if I do this, some text gets placed outside the main bounding box:
Is it possible to tell from this limited portion of the code what is going wrong here?
It happens because you have no style to "tell" your table to fill the full width.
Try adding the following stylesheet:
<style>
table { width: 100%; }
</style>

View Change "On Submit" Rails

Is there a way to do this without javascript?
Let me explain. I have two models, users and dogs.
I'm using the index as a home page for when the users are logged in.
There is a form that searches dogs (not by any information the user enters, it's prepopulated with dog data), but the index is listing the result of my search query on page load. I'd like it to only show when the button is submitted.
I'll show some code.
controller for dog:
def index
if current_user
#user = current_user
#breed = #user.dogs.first.primarybreed
# params[:search] = #breed
#dog = Dog.search(params[:search]).sample
end
end
view:
%= form_tag dogs_path, :method => :get do %>
<%= hidden_field_tag :search, #breed %>
<%= image_submit_tag("/images/greenadd.png", size: "10x10", :name => nil) %> New Doggy Playdate!
<% end %>
<div id="dogswrap">
<h1><%= current_user.dogs[0].name %> Should Meet With...</h1>
<table>
<tbody>
<tr>
<td><%= image_tag #dog.image.url(:medium) %></td>
<td><%= #dog.name %></td>
<td><%= #dog.nick %></td>
<td><%= #dog.primarybreed %></td>
<td><%= #dog.secondarybreed %></td>
<td><%= #dog.age %></td>
<td><%= #dog.weight %></td>
<td><%= link_to "#{#dog.user.name}" %> </td>
<td><%= image_tag #dog.user.image.url(:thumb) %> </td>
</td>
</tr>
</tbody>
</table>
Model
def self.search(search)
find(:all, :conditions => ['primarybreed LIKE ?', "%#{search}%"])
end
I would like to have an if statement that shows the table only after the submit has been pushed.
If there is another way to do this, I'd appreciate it.
Thanks!
EDIT: Still working on this. May use a render..?
You can just use where in place of the way you're currently doing it find and conditions
def self.search(search)
where('primarybreed LIKE ?', "%#{search}%")
end
One thing I've learnt when using the LIKE function in development for SQLite3 and LIKE in PostgreSQL for production, was LIKE is case-insensitive in SQLite3 but not for PSQL. You would need to use ILIKE for that.
To your main question. This is how I would do it.
def index
if current_user
#user = current_user
#dogs = Dog.search(params[:search]) if params[:search].present?
end
end
For you views, start moving everything into a partial. _form.html.erb and _dog.html.erb
form
<%= form_tag dogs_path, :method => :get do %>
<%= text_field_tag :search %>
<%= image_submit_tag("/images/greenadd.png", size: "10x10", :name => nil) %> New Doggy Playdate!
<% end %>
dog
<td><%= image_tag dog.image.url(:medium) %></td>
<td><%= dog.name %></td>
<td><%= dog.nick %></td>
<td><%= dog.primarybreed %></td>
<td><%= dog.secondarybreed %></td>
<td><%= dog.age %></td>
<td><%= dog.weight %></td>
<td><%= link_to "#{dog.user.name}" %> </td>
<td><%= image_tag dog.user.image.url(:thumb) %></td>
Notice removed #dog and replaced with dog
Index view
<%= render 'form' %>
<div id="dogswrap">
<h1><%= #user.dogs[0].name %> Should Meet With...</h1>
<table>
<tbody>
<tr>
<% if #dogs %>
<%= render #dogs %>
<% end %>
</tr>
</tbody>
</table>
changed current_user.dogs[0].. to #user.dogs[0].. Also made a conditional for if the #dogs instance variable is truthy. If it is, then render will render out the _dog.html.erb partial for each dog in the #dogs collection

Rails 3 - tables in html.erb

I'd like to view data from different database-tables in a view with tables like this picture shows:
I'm familiar with HTML tags <table>, <td> and <tr>, but I'm having trouble with multiple queries in a column.
<table>
<tr>
<th>Skills </th>
<th>Expected-qualifications</th>
<th>Current-qualifications</th>
</tr>
<% #employee.position.skills.each do |skill| %><% #employee.position.expected_qualifications.each do |expected_qualification| %><% #employee.current_qualifications.each do |current_qualification| %>
<tr>
<td><%= skill.kategorien %></td>
<td><%= expected_qualification.sollqualifikation %></td>
<td><%= current_qualification.istqualifikation %></td>
</tr>
<% end %><% end %><% end %>
</table>
This code looks like this:
As you can see, the skills, expected-qualifications, and current-qualifications repeat.
My question: How should the codes be ordered in the table so it will look the way I want it to?
Try zip:
<% #employee.position.skills.zip(#employee.position.expected_qualifications,#employee.current_qualifications).each |skill expected_qualification current_qualification| %>
<tr>
<td><%= skill.kategorien %></td>
<td><%= expected_qualification.sollqualifikation %></td>
<td><%= current_qualification.istqualifikation %></td>
</tr>
<% end %>
if there is REALLY can be more than one skill, expected_qualification and current_qualification so you use has_many assosiation forposition
<tr>
<td><%= #employee.position.skills.map(&:kategorien).join(", ") %></td>
<td><%= #employee.position.expected_qualifications.map(&:sollqualifikation).join(", ") %></td>
<td><%= #employee.current_qualifications.map(&:istqualifikation).join(", ") %></td>
</tr>
Otherwise you should use has_one association