How do I use Ruby in an HTML document - html

I have the following HTML in my index file.
<% #posts.each_with_index do |post, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>
I have a function called is_eligible which takes post.team_name as an argument. I want to run it each "do iteration" and skip all three printing steps if it returns false
I'm not sure how to
include the file is_eligible is in and
structure the code within the html somewhere.
Any ideas?

# The view
<% #posts.each_with_index do |post, index| %>
<% unless post.is_eligible? %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>
<% end %>
# The model post.rb
def is_eligible?
# and there you use self.name to access the name
end

<% #posts.select{|p| is_eligible(p. team_name)}.each_with_index do |post, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>

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| %>

Rails : Trying to push data from one table to another

In my rails application I require that the parameters from Approvals to be copied over to Rotas.
When clicking on the link to the Push action on the Index Page I am greeted with this error : param is missing or the value is empty: approval
My Approvals Index Page
<% if current_user.admin %>
<h1>Listing approvals</h1>
<% #approvals.each do |approval| %>
<table>
<tr>
<th></th>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
<tr>
<th>Louise</th>
<td><%= approval.grid11 %></td>
<td><%= approval.grid12 %></td>
<td><%= approval.grid13 %></td>
<td><%= approval.grid14 %></td>
<td><%= approval.grid15 %></td>
<td><%= approval.grid16 %></td>
<td><%= approval.grid17 %></td>
</tr>
<tr>
<th>Ruairi</th>
<td><%= approval.grid21 %></td>
<td><%= approval.grid22 %></td>
<td><%= approval.grid23 %></td>
<td><%= approval.grid24 %></td>
<td><%= approval.grid25 %></td>
<td><%= approval.grid26 %></td>
<td><%= approval.grid27 %></td>
</tr>
<tr>
<th>Gavin</th>
<td><%= approval.grid31 %></td>
<td><%= approval.grid32 %></td>
<td><%= approval.grid33 %></td>
<td><%= approval.grid34 %></td>
<td><%= approval.grid35 %></td>
<td><%= approval.grid36 %></td>
<td><%= approval.grid37 %></td>
</tr>
<tr>
<th>Conn</th>
<td><%= approval.grid41 %></td>
<td><%= approval.grid42 %></td>
<td><%= approval.grid43 %></td>
<td><%= approval.grid44 %></td>
<td><%= approval.grid45 %></td>
<td><%= approval.grid46 %></td>
<td><%= approval.grid47 %></td>
</tr>
<tr>
<th>Michael K</th>
<td><%= approval.grid51 %></td>
<td><%= approval.grid52 %></td>
<td><%= approval.grid53 %></td>
<td><%= approval.grid54 %></td>
<td><%= approval.grid55 %></td>
<td><%= approval.grid56 %></td>
<td><%= approval.grid57 %></td>
</tr>
<tr>
<th>Emily</th>
<td><%= approval.grid61 %></td>
<td><%= approval.grid62 %></td>
<td><%= approval.grid63 %></td>
<td><%= approval.grid64 %></td>
<td><%= approval.grid65 %></td>
<td><%= approval.grid66 %></td>
<td><%= approval.grid67 %></td>
</tr>
<tr>
<th>Stephen W</th>
<td><%= approval.grid71 %></td>
<td><%= approval.grid72 %></td>
<td><%= approval.grid73 %></td>
<td><%= approval.grid74 %></td>
<td><%= approval.grid75 %></td>
<td><%= approval.grid76 %></td>
<td><%= approval.grid77 %></td>
</tr>
<tr>
<th>Niamh</th>
<td><%= approval.grid81 %></td>
<td><%= approval.grid82 %></td>
<td><%= approval.grid83 %></td>
<td><%= approval.grid84 %></td>
<td><%= approval.grid85 %></td>
<td><%= approval.grid86 %></td>
<td><%= approval.grid87 %></td>
</tr>
<tr>
<th>Josh</th>
<td><%= approval.grid91 %></td>
<td><%= approval.grid92 %></td>
<td><%= approval.grid93 %></td>
<td><%= approval.grid94 %></td>
<td><%= approval.grid95 %></td>
<td><%= approval.grid96 %></td>
<td><%= approval.grid97 %></td>
</tr>
<tr>
<th>Aidan</th>
<td><%= approval.grid101 %></td>
<td><%= approval.grid102 %></td>
<td><%= approval.grid103 %></td>
<td><%= approval.grid104 %></td>
<td><%= approval.grid105 %></td>
<td><%= approval.grid106 %></td>
<td><%= approval.grid107 %></td>
</tr>
<tr>
<th>Courtney</th>
<td><%= approval.grid111 %></td>
<td><%= approval.grid112 %></td>
<td><%= approval.grid113 %></td>
<td><%= approval.grid114 %></td>
<td><%= approval.grid115 %></td>
<td><%= approval.grid116 %></td>
<td><%= approval.grid117 %></td>
</tr>
</table>
<% end %>
<table>
<thead>
<tr>
<th>Week</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #approvals.each do |approval| %>
<tr>
<td><%= approval.week %></td>
<td><%= link_to 'Approve This', approvals_push_path(:id => approval.id) %></td>
<td><%= link_to 'Show', approval %></td>
<td><%= link_to 'Edit', edit_approval_path(approval) %></td>
<td><%= link_to 'Destroy', approval, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<% end %>
<%= link_to 'New Approval', new_approval_path %>
My ApprovalsController
class ApprovalsController < ApplicationController
before_action :set_approval, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
respond_to :html
def index
#approvals = Approval.all
respond_with(#approvals)
end
def push
#approval = Approval.find(params[:id])
#rotum = Rotum.find_by_id(#approval.rotumid)
#rotum.update_attributes(push_params)
end
def show
respond_with(#approval)
end
def new
#rota = Rotum.find(params[:id])
#approval = Approval.new
respond_with(#approval)
end
def edit
end
def create
#approval = Approval.new(approval_params)
#approval.save
respond_with(#approval)
end
def update
#approval.update(approval_params)
respond_with(#approval)
end
def destroy
#approval.destroy
respond_with(#approval)
end
private
def set_approval
#approval = Approval.find(params[:id])
end
def set_rotum
#rotum = Rotum.find(:rotumid)
end
def approval_params
params.require(:approval).permit(:week, :grid11, :grid12, :grid13, :grid14, :grid15, :grid16, :grid17, :grid21, :grid22, :grid23, :grid24, :grid25, :grid26, :grid27, :grid31, :grid32, :grid33, :grid34, :grid35, :grid36, :grid37, :grid41, :grid42, :grid43, :grid44, :grid45, :grid46, :grid47, :grid51, :grid52, :grid53, :grid54, :grid55, :grid56, :grid57, :grid61, :grid62, :grid63, :grid64, :grid65, :grid66, :grid67, :grid71, :grid72, :grid73, :grid74, :grid75, :grid76, :grid77, :grid81, :grid82, :grid83, :grid84, :grid85, :grid86, :grid87, :grid91, :grid92, :grid93, :grid94, :grid95, :grid96, :grid97, :grid101, :grid102, :grid103, :grid104, :grid105, :grid106, :grid107, :grid111, :grid112, :grid113, :grid114, :grid115, :grid116, :grid117, :rotumid)
end
def push_params
params.require(:approval).permit(:grid11, :grid12, :grid13, :grid14, :grid15, :grid16, :grid17, :grid21, :grid22, :grid23, :grid24, :grid25, :grid26, :grid27, :grid31, :grid32, :grid33, :grid34, :grid35, :grid36, :grid37, :grid41, :grid42, :grid43, :grid44, :grid45, :grid46, :grid47, :grid51, :grid52, :grid53, :grid54, :grid55, :grid56, :grid57, :grid61, :grid62, :grid63, :grid64, :grid65, :grid66, :grid67, :grid71, :grid72, :grid73, :grid74, :grid75, :grid76, :grid77, :grid81, :grid82, :grid83, :grid84, :grid85, :grid86, :grid87, :grid91, :grid92, :grid93, :grid94, :grid95, :grid96, :grid97, :grid101, :grid102, :grid103, :grid104, :grid105, :grid106, :grid107, :grid111, :grid112, :grid113, :grid114, :grid115, :grid116, :grid117)
end
def rotum_params
params.require(:rotum).permit(:week, :grid11, :grid12, :grid13, :grid14, :grid15, :grid16, :grid17, :grid21, :grid22, :grid23, :grid24, :grid25, :grid26, :grid27, :grid31, :grid32, :grid33, :grid34, :grid35, :grid36, :grid37, :grid41, :grid42, :grid43, :grid44, :grid45, :grid46, :grid47, :grid51, :grid52, :grid53, :grid54, :grid55, :grid56, :grid57, :grid61, :grid62, :grid63, :grid64, :grid65, :grid66, :grid67, :grid71, :grid72, :grid73, :grid74, :grid75, :grid76, :grid77, :grid81, :grid82, :grid83, :grid84, :grid85, :grid86, :grid87, :grid91, :grid92, :grid93, :grid94, :grid95, :grid96, :grid97, :grid101, :grid102, :grid103, :grid104, :grid105, :grid106, :grid107, :grid111, :grid112, :grid113, :grid114, :grid115, :grid116, :grid117)
end
end
Any help would be greatly appreciated!
Similarly to the following questions:
Param is missing or the value is empty user Rails 4
and
Param is missing or the value is empty
your issue seems to be caused by the way you use strong parameters in a controller. Basically you don't pass required approval field in a request to approvals_push_path, just send an id of an approval.
def push
#approval = Approval.find(params[:id])
#rotum = Rotum.find_by_id(#approval.rotumid)
# Throws error, because there is no approval param in params
#rotum.update_attributes(push_params)
end
To avoid the error you can select the fields to copy from the #approval object:
def push
#approval = Approval.find(params[:id])
#rotum = Rotum.find_by_id(#approval.rotumid)
# We have to select which attributes we want to copy.
#rotum.update_attributes(attrs_for_push)
end
def attrs_for_push
# Let's select only attributes we need.
#approval.attributes.slice(:week, :grid11, :grid12, :grid13, :grid14, :grid15, :grid16, :grid17, :grid21, :grid22, :grid23, :grid24, :grid25, :grid26, :grid27, :grid31, :grid32, :grid33, :grid34, :grid35, :grid36, :grid37, :grid41, :grid42, :grid43, :grid44, :grid45, :grid46, :grid47, :grid51, :grid52, :grid53, :grid54, :grid55, :grid56, :grid57, :grid61, :grid62, :grid63, :grid64, :grid65, :grid66, :grid67, :grid71, :grid72, :grid73, :grid74, :grid75, :grid76, :grid77, :grid81, :grid82, :grid83, :grid84, :grid85, :grid86, :grid87, :grid91, :grid92, :grid93, :grid94, :grid95, :grid96, :grid97, :grid101, :grid102, :grid103, :grid104, :grid105, :grid106, :grid107, :grid111, :grid112, :grid113, :grid114, :grid115, :grid116, :grid117)
end
You may want to move copying logic from the controller to a model, or a seperate utility service object.
ActiveRecord::AttributeMethods#attributes documentation.

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

Displaying only one record in the table

I want to display only a single row in the table. My controller for the same looks like this:
def Add
#item= Item.find(params[:id])
end
and my table row looks like this.. Isn't there a substitute for the method .each to just display one record?
<% #item.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<td><%= item.quantity %></td>
<td><%= item.quantity * item.price %></td>
<td>
<% link_to("update", '#', :class => 'action update') %>
<% link_to("X", '#', :class => 'action delete') %>
</td>
</tr>
<% end %>
In this case you don't need loop. U can directly create table like this
<tr>
<td><%= #item.name %></td>
<td><%= #item.price %></td>
<td><%= #item.quantity %></td>
<td><%= #item.quantity * #item.price %></td>
<td>
<% link_to("update", '#', :class => 'action update') %>
<% link_to("X", '#', :class => 'action delete') %>
</td>
</tr>
#item= Item.find(params[:id])
Here, #item will be an object of class Item. So You don't need to Iterate the Object with 'each'. You can directly access the Item attribute by #item object.
#item.name
#item.price
#item.quantity.....

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