Currently unable to pass information from the cycles controller to the cycle_nominees_table.html.erb using partials.
The data is defined in my controller show method.
And the show method has information in the #nominations_data and #nominations_team_data.
Why cant my _cycles_nominations_table.html.erb access nominations_data thats being passed into the partial?
Ive been using this link for direction.
Code Below:
app/controllers/cycles_controller.rb
def show
...
#nominations_data = {}
#nominations.each do |nomination|
#nominations_data[nomination.for_user_id] ||= {}
#nominations_data[nomination.for_user_id][:nominee] ||= nomination.for_user
if user_is_admin
#nominations_data[nomination.for_user_id][:nominations] ||= []
#nominations_data[nomination.for_user_id][:nominations].push nomination
end
if user_is_admin_or_reviewer
#nominations_data[nomination.for_user_id][:count] ||= 0
#nominations_data[nomination.for_user_id][:count] += 1
end
end
#nominations_data = #nominations_data.sort_by { |_nid, nomination| nomination[:nominee].name }
#nominations_team_data = {}
#team_nominations.each do |nomination|
#nominations_team_data[nomination.for_user_id] ||= {}
#nominations_team_data[nomination.for_user_id][:nominee] ||= nomination.for_user
if user_is_admin
#nominations_team_data[nomination.for_user_id][:nominations] ||= []
#nominations_team_data[nomination.for_user_id][:nominations].push nomination
end
if user_is_admin_or_reviewer
#nominations_team_data[nomination.for_user_id][:count] ||= 0
#nominations_team_data[nomination.for_user_id][:count] += 1
end
end
#nominations_team_data = #nominations_team_data.sort_by { |_nid, nomination| nomination[:nominee].name }
end
app/views/cycles/show.html.erb
<div class="container-float-right-small" id="cycle-nominees-section">
<h2 class="inline">Nominees for <%= #cycle.name %></h2>
<h4 class="inline"> - (<%= #cycle.votes_per_reviewer %> votes allowed)</h4>
<h5> Indivdual Nominees: </h5>
<!-- <%= render "cycles/cycle_nominees_table" %>-->
<%= render partial: "cycles/cycle_nominees_table", locals: { nominations_data: #nominations_data } %>
<h5> Team Nominees: </h5>
<!-- <%= render "cycles/cycle_nominees_table" %>-->
<%= render partial: "cycles/cycle_nominees_table", locals: { nominations_data: #nominations_team_data } %>
</div>
app/views/cycles/_cycle_nominees_table.html.erb
<% if nominations_data.empty? %>
<th class="center" colspan="4"><h3>There are no nominees <%= "for cycle '#{#cycle.name}'" if #cycle %></th>
<% end %>
</thead>
<tbody>
<% nominations_data.each do |nid, data| %>
<tr id='<%= "nominee-row-#{nid}" %>' class="nominee-row">
<% if user_is_reviewer && #cycle.voting_open %>
<td class="center">
<% nomination_icon_class = "voted_for" if Vote.where(cycle_id: #cycle.id, by_user_id: current_user.id, for_user_id: data[:nominee].id).count > 0 %>
<span class="glyphicon glyphicon-check pointer <%= nomination_icon_class %>" id=<%= "vote_for_nominee_#{nid}" %> onclick="<%= "Vote.send_vote_data(this, #{data[:nominee].id}, #{#cycle.id})" %>"></span>
</td>
<% end %>
Related
I am using filterrific gem to add filters in my app. I have parents table and children table. On the children_list page which displays list of all the children with their firstname and their parent's firstname. The issue I am facing is in the search query I want to add the parent.firstname search as well for filterrific. I tried adding a join as below:-
num_or_conds = 2
joins(child: :parent).where(
terms.map { |term|
"(LOWER(children.firstname) LIKE ?) OR (LOWER(parents.firstname) LIKE ?) "
But this didnt do the job. Any idea how this can be achieved.
parent.rb
has_many :children
child.rb
belongs_to :parent
filterrific(
available_filters: [
:search_query,
:with_clinic_date
]
)
scope :search_query, lambda { |query|
return nil if query.blank?
terms = query.downcase.split(/\s+/)
terms = terms.map { |e|
(e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
num_or_conds = 2
where(
terms.map { |term|
"(LOWER(children.firstname) LIKE ?) OR (LOWER(parents.firstname) LIKE ?)"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conds }.flatten
)
}
scope :with_clinic_date, lambda { |ref_date|
where('children.clinic_date = ?', ref_date)
}
end
_children.html.erb
<h1>Children</h1>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Parent First Name</th>
<th>Child firstname</th>
</tr>
</thead>
<tbody>
<% #children.each do |child| %>
<tr>
<td><%=child.parent.firstname %></td>
<td><%=child.firstname %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
children-list.html.erb
<%= form_for_filterrific #filterrific do |f| %>
<div class="row">
<div class="col-sm-3">
Search
<%= f.text_field(
:search_query,
class: 'filterrific-periodically-observed form-control'
) %>
</div>
<div class="col-sm-3">
Request Date
<%= f.date_field(:with_clinic_date, class: 'js-datepicker form-control') %>
</div>
<div class="col-sm-3">
<br>
<%= link_to(
'Reset filters',
reset_filterrific_url,
) %>
</div>
</div>
<%= render_filterrific_spinner %>
<% end %>
<%= render( partial: 'children/children_list') %>
children.js.erb
<% js = escape_javascript(
render(partial: 'children/children_list')
) %>
$("#filterrific_results").html("<%= js %>");
AFAIK, you can't filter two separate classes on the same page. It will use the last defined filterrific instance. When I ran into this problem, I used remote forms with custom action/routes
# routes.rb
resources :parent do
get :filter_parents
resources: children do
get :filter_children
end
end
And then the controllers..
# parents_controller.rb
def index
parents_filter # this would be a helper method running your filter queries
end
def filter_parents
parents_filter # this would be a helper method running your filter queries
end
The children's controller would look similar, just different named helper method/custom action.
And then use a partial for the table. Target the table's container, and use a filter_parents.js.erb and filter_childrens.js.erb file
$('#parents-table').html('<%= escape_javascript render 'path/to/partial'%>')
// same in childrens.js.erb, just target the appropriate table
I'm currently trying to display the first record. The record is updated when someone decides to buy a wine. How would I get the first record for the specific wine ?
I thought of using .find(1)? but how would implement this into my controller ?
My current controller:
def create
wine = Wine.find(params[:wine_id])
if current_user == wine.user
flash[:alert] = "Du kannst nicht deinen eigenen Wein kaufen!"
else
start_date = Date.parse(reservation_params[:start_date])
#reservation = current_user.reservations.build(reservation_params)
#reservation.wine = wine
#reservation.price = wine.price
#reservation.in_stock = wine.in_stock - #reservation.bottle
#reservation.total = wine.price * #reservation.bottle
wine.update(in_stock: #reservation.in_stock)
# #reservation.save
if #reservation.save
if wine.Reservieren?
flash[:notice] = "Reserviert!"
else
#reservation.Versendet!
flash[:notice] = "Eroflgreich bestellt!"
end
else
flash[:alert] = "Can't make a reservation!"
end
end
redirect_to wine
end
My current view:
<% #wines.each do |wine| %>
<div class="row">
<div class="col-md-3">
<%= link_to wine.wine_name, wine_path(wine) %>
</div>
<div class="col-md-3">
<%= wine.in_stock %> <!-- Display first record -->
</div>
<div class="col-md-3">
<% if !wine.in_stock.blank? %>
Aktiv
<% else %>
Nicht Aktiv
<% end %>
</div>
<div class="col-md-3 right">
<%= link_to "Update", details_wine_path(wine), class: "btn btn-form" %>
</div>
</div><!-- row -->
<hr/>
<% end %>
Maybe you can explain a little better your code, because you receive #wines in your view and it is not coming from this controller action that you posted.
But if you want to take the first register you can basically call wines.first.
You would change from <%= wine.in_stock %> to <%= #wines.first %> for example.
Here is some thread related:
Display first record in rails
Im currently working in an Rails 5 application where you can search for a first name or last name and records of the customers of that account would be displayed. However I am getting a Nil object return from search algorithm.
customers_controller:
class CustomersController < ApplicationController
def index
if params[:keywords].present?
#keywords = params[:keywords]
customer_search_term = CustomerSearchTerm.new(#keywords)
#customer = Customer.where(
customer_search_term.where_clause,
customer_search_term.where_args).
order(customer_search_term.order)
else
#customers = []
end
end
end
As you can see if there is no records found is suppose to return an empty array but is returning a Nil object.
customers/index.html.erb
[![<header>
<h1 class="h2">Customer Search</h1>
</header>
<section class="search-form">
<%= form_for :customers, method: :get do |f| %>
<div class="input-group input-group-lg">
<%= label_tag :keywords, nil, class: "sr-only" %>
<%= text_field_tag :keywords, nil,
placeholder: "First Name, Last Name or Email Address",
class: "form-control input-lg" %>
<span class="input-group-btn">
<%= submit_tag "Find Customers", class: "btn btn-primary btn-lg" %>
</span>
</div>
<% end %>
</section>
<section class="search-results">
<header>
<h1 class="h3">Results</h1>
</header>
<table class="table table-striped">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Joined</th>
</tr>
</thead>
<tbody>
<% #customers.each do |customer| %>
<tr>
<td><%= customer.first_name %></td>
<td><%= customer.last_name %></td>
<td><%= customer.email %></td>
<td><%= l customer.created_at.to_date %></td>
</tr>
<% end %>
</tbody>
</table>
</section>][1]][1]
The first thing you should understand is that instance variables return nil if they haven't been set. If you say #fake_var == nil it will be true if you never defined #fake_var before this. You can contrast this with regular local variables, which will raise a NoMethodError if you try and use them before they're defined. For example, puts(fake_var) will raise a NoMethodError for fake_var.
Now look at your template. No matter what it will loop over #customers. If #customers has not been set, you'll see a NoMethodError because you can't call each on nil.
Finally, look at your controller action:
def index
if params[:keywords].present?
#keywords = params[:keywords]
customer_search_term = CustomerSearchTerm.new(#keywords)
#customer = Customer.where(
customer_search_term.where_clause,
customer_search_term.where_args).
order(customer_search_term.order)
else
#customers = []
end
end
Specifically the case when params[:keywords].present?. You never set #customers in this case so it will be nil when the template tries to access it.
I think if you simply replaced #customer = with #customers = it would solve your problem.
you can force it to return array using #to_a which converts nil to empty array
def index
return [] unless params[:keywords]
#keywords = params[:keywords]
customer_search_term = CustomerSearchTerm.new(#keywords)
#customer = Customer.where(
customer_search_term.where_clause,
customer_search_term.where_args).
order(customer_search_term.order
).to_a
end
https://apidock.com/ruby/Array/to_a
For my final project I need to create two radio buttons with a submit button that will display my GPA for all my classes and those in my major. I can have them displayed easily but am stuck when I need to use radio buttons to display only one
<h2>Please select which GPA you would like to view:</h2>
<%= form_tag("/trans/transcript", :method => "get") do %>
<table>
<tr>
<th>Major Credits</th>
<th>All Credits</th>
</tr>
<tr>
<td><%= radio_button_tag(:gpa, "Major") %></td>
<td><%= radio_button_tag(:gpa, "All") %> </td>
</tr>
</table>
<%= submit_tag("View GPA") %>
<% end %>
<p> Major credits GPA <%= #transcript.GPA_for_major %>
<p> All credits GPA <%= #transcript.GPA_for_non_major %>
Everything appears fine but I'm not sure how to set up the controller to say if he clicks the major gpa radio button and clicks "View GPA" this <%= #transcript.GPA_for_major %> should display
SHORT ANSWER
Make an instance variable #gpa in TransController's transcript action
In the transcript action, check for params[:gpa]
set #gpa & #transcript to nil if params[:gpa].nil?
set #gpa to params[:gpa] and #transcript accordingly if params[:gpa] is not nil
In the view/trans/transcript.html.erb
If #gpa is nil, display nothing
If #gpa == "Major" show #transcript.GPA_for_major
If #gpa == "All" show #transcript.GPA_for_non_major
LONG ANSWER
Assuming that the controller is trans and the action is transcript, here is what you should do in TransController
class TransController < ApplicationController
def transcript
if params[:gpa].nil?
#transcript = #gpa = nil
else
#gpa = params[:gpa]
#transcript = ... # Find your transcript here
end
end
# ... other actions
# ... other actions
# ... other actions
end
and here is how the trans/transcript.html.erb view should look like -
<h2>Please select which GPA you would like to view:</h2>
<%= form_tag('trans/transcript', :method => "get") do %>
<table>
<tr>
<th>Major Credits</th>
<th>All Credits</th>
</tr>
<tr>
<td>Major<%= radio_button_tag(:gpa, "Major") %></td>
<td>All<%= radio_button_tag(:gpa, "All") %> </td>
</tr>
</table>
<%= submit_tag("View GPA") %>
<% end %>
<% if #gpa == "Major" %>
<%= #transcript.GPA_for_major %>
<% elsif #gpa == "All" %>
<%= #transcript.GPA_for_major %>
<% end %>
I am trying to use the Quicksand jQuery plugin to decorate the response of an AJAX call in Rails:
Link:
<% Project.services.each_with_index do |service, index| %>
<%= link_to_function(service, "regatherProjects('#{service}', #{index})", :id => "service_link_#{index}") %>
<%= " / " if !index.eql?(Project.services.length - 1) %>
<% end %>
JS:
function regatherProjects(service_name, service_id) {
$.get("/index_project_update", { service: service_name }, function(data) {
$('#home_projects').quicksand($(data), {
adjustHeight: 'dynamic'
});
});
$('#home_services').find('a').removeClass("on");
$('#service_link_' + service_id).addClass("on");
};
Controller:
def index_project_update
if params[:service] && !params[:service].blank?
#projects = Project.highlighted.where(:service => params[:service])
else
#projects = Project.highlighted
end
end
View:
$("#home_projects").replaceWith("<%= j(render('projects')) %>")
Projects:
<div id="home_projects">
<% if #projects.empty? %>
<p>No projects. <%= link_to_function("View All", "regatherProjects('')") %>.</p>
<% else %>
<ul class="all_projects">
<% #projects.each_with_index do |project, index| %>
<li data-id="<%= project.customer.name.underscore.downcase %>_<%= project.name.underscore.downcase %>">
<%= link_to(image_tag(project.project_images.first.image.home), project) %>
</li>
<% end %>
</ul>
<% end %>
</div>
I think the problem is that the data returned by the AJAX request (see below) is not in the format that Quicksand expects it to be in but I'm not sure how to extrapolate the relevant information from the response. The response from my AJAX request is something like:
$("#home_projects").replaceWith("<some><html><that><i><need>")
So, the problem is that it's using the above code as the destination code for the Quicksand call when it should be using the replacement HTML:
<some><html><that><i><need>
The AJAX request works perfectly fine, but I need to integrate Quicksand into here.
I need:
a better way to do this or;
a way to extrapolate the argument for replaceWith() in the aforementioned code to use as the destination code for Quicksand.
If I've omitted any information you think you need to help, please ask.
This is the first question I've asked on Stack Overflow so I feel obligated to apologise for any misusage.
If anyone runs into a similar issue, the following worked for me in Chrome, Firefox, IE9, IE8 and IE7.
Link:
<% Project.services.each_with_index do |service, index| %>
<%= link_to_function(service, "regatherProjects('#{service}', #{index})", :id => "service_link_#{index}") %>
<%= " / " if !index.eql?(Project.services.length - 1) %>
<% end %>
JS:
function regatherProjects(service_name, service_id) {
$.get("/index_project_update.html", { service: service_name }, function(data) {
$('.all_projects').quicksand($(data).find('li'), {
adjustHeight: 'dynamic'
});
});
$('#home_services').find('a').removeClass("on");
$('#service_link_' + service_id).addClass("on");
};
Route:
match '/index_project_update' => 'application#index_project_update', :defaults => { :format => 'html' }
Controller:
def index_project_update
if params[:service] && !params[:service].blank?
#projects = Project.highlighted.where(:service => params[:service])
else
#projects = Project.highlighted
end
render :layout => false
end
View:
<%= render('application/index/projects') %>
Projects:
<div id="home_projects">
<% if #projects.empty? %>
<ul class="all_projects">
<li data-id="project">No projects. <%= link_to_function("View All", "regatherProjects('')") %>.</li>
</ul>
<% else %>
<ul class="all_projects">
<% #projects.each_with_index do |project, index| %>
<li data-id="project_<%= project.id %>">
<%= link_to(image_tag(project.project_images.first.image.home, :alt => (project.customer.name + " - " + project.name), :title => (project.customer.name + " - " + project.name)), project) %>
</li>
<% end %>
</ul>
<% end %>
</div>