Anchor while itering - Rails - ruby-on-rails

I am trying to make an anchor in an index page while itering through a list of elements.
On the home page I display each element with a 'more info' button. When clicked it goes to the index of detailed elements. I tried the code below, but it doesn't work.
link_to 'More info', tours_path(#tour, :anchor => "#{tour.name}"
I also tried, without success:
link_to 'More info', tours_path(#tour, :anchor => tour.name
Here is the code of home.html.erb :
<section id="walking-tours">
<h2 class="text-center">Lisbon walking tours </h2>
<div class="walking-tours d-flex">
<% #tours.each do |tour| %>
<div class="walking-tours-cards item">
<% photo = tour.photos[0] %>
<%= cl_image_tag photo.key, height: 300, width: 300, crop: :fill if tour.photos.attached? %>
<h3> <%= tour.name %> </h3>
<p> <%= tour.description %> </p>
<%= link_to 'More info', tours_path(#tour, :anchor => "#{tour.name}"), class: 'btn btn-outline-secondary' %>
</div>
<% end %>
</div>
</section>
and the code in index.html.erb
<div class="index-tours">
<div class="tours-title">
<h1 >WALKING TOURS</h1>
</div>
<div class="tours-list">
<% #tours.each do |tour| %>
<div class="walking-tours-details ">
<div class="walking-tours-info">
<h3> <%= tour.name %> </h3>
<p> <%= tour.full_information %> </p>
<p><b>Duration</b> -<%= tour.duration %> </p>
<p><b>Meeting Point</b> - <%= tour.meeting_point %> </p>
</div>
<div class="tours-pics">
<% tour.photos.each do |photo| %>
<%= cl_image_tag photo.key, height: 220, width: 220, crop: :fill, :class => "tours-img" %>
<% end %>
</div>
</div>
<% if logged_in? %>
<%= link_to 'Edit', edit_tour_path(tour), :class => "tours-links" %> <%= link_to 'Destroy', tour, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
<% end %>
</div>
For now, all 'more info' buttons go to the first element of the list (at the top of the index page).
Any idea to help? Thanks!!
Cécile

I found a solution by generating a dynamic ID:
<% tour_link = tour.name %>
<% tour_identification = tour_link.gsub(/\s+/, '') %>
<div id= <%= tour_identification %> >
and for the link_to :
<%= link_to 'More info', tours_path(tour, anchor: tour_identification) %>
But I still wonder if the anchor could work without an ID.

Related

Rails 7 turbo_frame inline editing Response has no matching turbo-frame

Hello I have an rails 7 application where i want to inline edit an action item, and on the action_item.status turbo frame I get this error Response has no matching <turbo-frame id="status_action_item_8c36e8a6-70a5-4417-864b-198788f984b3"> element
I have several identical frames(different domID) which have no issue.
I have been following along with some great turbo content from gorails and hotrails and this is really puzzeling me, i am hoping i am close and it just needs a set of eyes.
Index
<div class="row justify-content-center">
<div class="col-12">
<div class="card shadow border-dark border-2 rounded-2">
<div class="card-header bg-dark border-secondary border-bottom border-2 d-flex justify-content-between align-items-center">
<div class="dropdown">
<button class="btn btn-lg btn-outline-light dropdown-toggle float-start" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false" target="_top">
Incomplete Tasks
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><%= link_to "Completed Tasks", completed_action_items_path, class: "dropdown-item"%></li>
<li><%= link_to "In Progress Tasks", in_progress_action_items_path, class: "dropdown-item"%></li>
</ul>
</div>
<%= link_to new_action_item_path, class: 'btn btn-outline-light float-end m-2' do %>
+
<% end %>
</div>
<div class="card-body m-0 p-0">
<div class="table-responsive">
<table class="table table-hover table-borderless m-0 p-0 align-middle">
<thead>
<tr class="table-dark">
<th style="width: 3%"></th>
<th class="" style="width: 30%"><span class="m-2">Name</span></th>
<th class="d-none d-md-table-cell" style="width: 27%"><span class="m-2">Meeting</span></th>
<th class="text-center" style="width: 10%"><span class="m-2">Status</span></th>
<th class="text-center d-none d-md-table-cell" style="width: 5%"><span class="m-2">Priority</span></th>
<th class="text-center" style="width: 5%"><span class="m-2">Due</span></th>
<th class="text-center" style="width: 10%"><span class="m-2">Actions</span></th>
</tr>
</thead>
<tbody>
<% #action_items.each do |action_item| %>
<tr>
<td>
<button class="btn btn-sm btn-outline-success rounded-3 border-0 float-end me-1"
type="button"
data-bs-toggle="tooltip"
data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
title="Complete task"
>
<i class="fas fa-check-circle text-success hide"></i>
</button>
</td>
<td class=""><small class="ms-2"><%= link_to action_item.name, action_item, class: "text-decoration-none text-dark" %></small></td>
<td class="d-none d-md-table-cell"><small class=""><%= action_item.meeting.name %></small></td>
<%= render 'status', action_item: action_item %>
<%= render 'priority', action_item: action_item %>
<%= render 'due', action_item: action_item %>
<%= render 'actions', action_item: action_item %>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
Status Partial
<td class="text-center">
<small class="">
<% frame_id = dom_id(action_item, "status") %>
<%= form_with model: action_item, class: '', data: { turbo_frame: frame_id } do %>
<%= turbo_frame_tag frame_id, class: 'inline-edit' do %>
<%= status_method(action_item) %>
<% end %>
<% end %>
</small>
</td>
Form
<%= form_with(model: action_item) do |form| %>
<% if action_item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(action_item.errors.count, "error") %> prohibited this action_item from being saved:</h2>
<ul>
<% action_item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="mb-3">
<% frame_id = dom_id(action_item, "name_turbo_frame") %>
<%= turbo_frame_tag frame_id do %>
<div class="input-group">
<%= form.text_field :name, class: 'form-control mw-100', placeholder: 'Description' %>
<%= form.button class: "btn btn-dark inline-action" do %>
<i class="fa-regular fa-floppy-disk"></i>
<% end %>
</div>
<% end %>
</div>
<% if #Meeting.present? %>
<%= form.hidden_field :meeting_id, value: #meeting.id %>
<% end %>
<% frame_id = dom_id(action_item, "attendee_turbo_frame") %>
<%= turbo_frame_tag frame_id do %>
<% if #action_item.meeting.present? %>
<div class="input-group">
<%= form.select :attendee_id, action_item.meeting.attendees.all.map { |p| [ p.user.email, p.id ] }, { include_blank: true}, { class: 'form-select' } %>
<%= form.button class: "btn btn-dark inline-action" do %>
<i class="fa-regular fa-floppy-disk"></i>
<% end %>
<%= form.hidden_field :status, value: "assigned" %>'
</div>
<% end %>
<% end %>
<div class="mb-3">
<% frame_id = dom_id(action_item, "action_item_priority_turbo_frame") %>
<%= turbo_frame_tag frame_id do %>
<div class="input-group">
<%= form.select(:priority, ActionItem.priorities.keys.map { |priority| [priority.titleize, priority]}, { include_blank: "Priority" }, { class: 'form-select' }) %>
<%= form.button class: "btn btn-dark inline-action" do %>
<i class="fa-regular fa-floppy-disk"></i>
<% end %>
</div>
<% end %>
</div>
<div class="mb-3">
<% frame_id = dom_id(action_item, "status") %>
<%= turbo_frame_tag frame_id do %>
<div class="input-group">
<%= form.select(:status, ActionItem.statuses.keys.map { |status| [status.titleize, status]}, { include_blank: "Status" }, { class: 'form-select'}) %>
<%= form.button class: "btn btn-dark inline-action" do %>
<i class="fa-regular fa-floppy-disk"></i>
<% end %>
</div>
<% end %>
</div>
<div class="mb-3">
<% frame_id = dom_id(action_item, "action_item_due_date_turbo_frame") %>
<%= turbo_frame_tag frame_id do %>
<div class="input-group">
<%= form.date_field :due, class: 'form-control' %>
<%= form.button class: "btn btn-dark inline-action" do %>
<i class="fa-regular fa-floppy-disk"></i>
<% end %>
</div>
<% end %>
</div>
<div class="mb-3">
<% if action_item.persisted? %>
<div class="float-end">
<%= link_to 'Destroy', action_item, method: :delete, class: "text-danger", data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
<%= form.submit class: 'btn btn-primary' %>
<% if action_item.persisted? %>
<%= link_to "Cancel", action_item, class: "btn btn-link" %>
<% else %>
<%= link_to "Cancel", action_items_path, class: "btn btn-link" %>
<% end %>
</div>
<% end %>
ActionItemsHelper
def status_method(action_item)
if action_item.status.present?
link_to action_item.status.titleize, edit_action_item_path(action_item), class: "btn btn-sm btn-outline-dark border-0"
else
link_to edit_action_item_path(action_item), class: "btn btn-sm btn-outline-dark border-0" do
content_tag(:i, "", class: ["fas", "fa-traffic-light", "fa-sm"] )
end
end
end
Thanks in advance

Ordering by category id and price on index page

I’m having trouble with ordering my houses. I have a houses that belong_to category (category has_many houses) and I want to order the houses on the index based on their category_id, but whatever I try it doesn’t seem to change the order. It’s a mystery to me. Maybe you could help me out?
class HousesController < ApplicationController
skip_before_action :authenticate_user!, only: [:index, :show]
before_action :set_house, only: [ :show, :edit, :update, :destroy, :inquiry ]
​
def index
# byebug
#houses = policy_scope(House).order(:name)
#houses = params.has_key?(:h) ? index_result(#houses) : #houses
# split_categories(#houses)
end
​
def index_result(houses)
# byebug
wall_thickness = params[:h].to_i
if wall_thickness == 1
houses.order(:name)
render "results_index_all"
elsif wall_thickness == 28 || 40
houses = houses.where(:wall => wall_thickness)
houses.order(:name)
render "results_index"
else
end
end
​
def split_categories(houses)
# #houses_1 = #houses.where(price_cents: 0...260000)
# #houses_2 = #houses.where(price_cents: 260000..1000000)
# byebug
category = Category.find_by(id: params[:category])
houses = #category.present? ? category.houses.order(price_cents: :asc) : houses
end
​
def show
end
​
def new
#house = House.new
authorize #house
end
​
def create
#house = House.new(house_params)
#house.user = current_user
authorize #house
if #house.save
redirect_to house_path(#house)
else
render :new
end
end
​
def edit
end
​
def update
if #house.update(house_params)
redirect_to house_path(#house)
else
render :edit
end
end
​
def destroy
#house.destroy
redirect_to root_path
end
​
private
​
def set_house
#house = House.friendly.find(params[:id])
authorize #house
end
​
def house_params
params.require(:house).permit(:sku, :name, :price_cents, :width, :length, :wall, pictures: [])
end
​
end
Below is the normal index where I split houses up in categories starting with category_id: 2 (normal garden houses), then 1 (gazebos) and finally 3 (garages). This is partial which is rendered app/views/houses/index.html.erb
<% if !#category %>
<%= render 'first-banner-home' %>
<div class="container catalog">
<%= render "filter_buttons" %>
<div class="row">
<% #houses.where(category_id: 2).each do |house| %>
<div class="col-sm-6 col-md-4">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<div class="container catalog">
<h3><%= t('home.gazebos') %></h3>
<div class="row">
<% #houses.where(category_id: 1).each do |house| %>
<div class="col-sm-6">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top-alt", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<div class="container catalog">
<h3><%= t('home.garages') %></h3>
<div class="row">
<% #houses.where(category_id: 3).each do |house| %>
<div class="col-sm-6">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top-alt", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render "last-banner" %>
<% elsif params[:h].present? %>
<%= render "results_index" %>
<% else %>
<%= render "category_index" %>
<% end %>
This is the index where the houses are filtered based on their wall thickness (see in app/controllers/houses_controller.rb ). This is where I want the house to order based on their category_id, which doesn’t work at the moment.
<% content_for(:title) do %><%= t('topbar.brand') %><% end %>
<% content_for(:og_site_name) do %><%= t('topbar.brand') %><% end %>
<% content_for(:description) do %><%= t('garden_houses.meta_description') %><% end %>
<% content_for(:og_image) do %><%= cl_image_path("https://res.cloudinary.com/www-ibizagardenhouses-com/image/upload/c_scale,h_630,w_1200/v1558696079/ibi-3/image.jpg") %><% end %>
<% content_for(:og_description) do %><%= t('garden_houses.meta_description') %><% end %>
<%= render 'first-banner-results' %>
<% if #houses.count >= 4 %>
<div class="container catalog">
<%= render "filter_buttons" %>
<div class="row">
<% #houses.each do |house| %>
<div class="col-sm-6 col-md-4">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render "last-banner" %>
<% else %>
<div class="container catalog">
<%= render "filter_buttons" %>
<div class="row">
<% #houses.each do |house| %>
<div class="col-sm-6">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top-alt", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render "last-banner" %>
<% end %>
This is exactly the same html as app/views/houses/results_index.html.erb , but where I show all the houses (params[h: 1]) in a layout that’s not the same as the home page. Same problem here as on the filtered results page.
<% content_for(:title) do %><%= t('topbar.brand') %><% end %>
<% content_for(:og_site_name) do %><%= t('topbar.brand') %><% end %>
<% content_for(:description) do %><%= t('garden_houses.meta_description') %><% end %>
<% content_for(:og_image) do %><%= cl_image_path("https://res.cloudinary.com/www-ibizagardenhouses-com/image/upload/c_scale,h_630,w_1200/v1558696079/ibi-3/image.jpg") %><% end %>
<% content_for(:og_description) do %><%= t('garden_houses.meta_description') %><% end %>
<%= render 'first-banner-all' %>
<% if #houses.count >= 4 %>
<div class="container catalog">
<%= render "filter_buttons" %>
<div class="row">
<% #houses.each do |house| %>
<div class="col-sm-6 col-md-4">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render "last-banner" %>
<% else %>
<div class="container catalog">
<%= render "filter_buttons" %>
<div class="row">
<% #houses.each do |house| %>
<div class="col-sm-6">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top-alt", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render "last-banner" %>
<% end %>
As you can see, there's a lot of duplication of code (not very DRY) and so I'd like a little help in getting more DRY and have the houses be ordered by category_id and then by price.
I'd appreciate your help.
It's not clear to me what you tried so far, but if all you need is to sort by category_id, I think you can define a scope within your house model as follow:
scope :sort_by_category_id_asc, ->{
order(arel_table[:category_id].asc)
}
Then you can use that scope as follow: House.where(...).sort_by_category_id_asc, also you can use desc, depends on how you want to order the result.
Hope this helps! 👍
I created a method that can sort houses by its category_id. If you want to sort it by other things just change category_id to your desired thing. You can sort it ascending or descending order. Here it is:
def sort_by_category_id(houses, order)
case order_by
when "asc"
houses = houses.sort_by{ |house|
house[:category_id]
}
when "desc"
house= house.sort_by{ |house|
house[:category_id]
}.reverse!
end
house
end
Don't forget to call this method wherever you want it to be worked. Below code will sort your houses by category_id and save it in sorted_houses variable. I
sorted_houses_asc = sort_by_category_id(#houses, "asc")
sorted_houses_desc = sort_by_category_id(#houses, "desc")
Hope this helped you somehow.

how to implement pagination with multistep?? OR is their any way to hold the previous page radio button value when using pagination

I am creating a rails quiz .. i have done everything the problem is that i want to implement pagination and when the user goes to next page the previous page values are lost. i am new to rails.
view page -- that show question paper
<div class="navbar navbar-fixed-top">
<h3><% if #paper.timing!=0 %><div id="time" class="bg"></div><%end%></h3>
</div>
<br>
<%= form_for Result.new do |f| %>
<div id="content">
<div class="inputs">
<div>
<div>
<div>
<div>
<% #questions.each_with_index do |question , i | %>
<%= f.hidden_field :userchoice_id, :value => session[:id] %>
<%= f.hidden_field :exam_id, :value => session[:exam_id] %><br>
<h3> <%= i + 1 %>.<%= question.content %></h3><br>
<%count = 0%>
<% a=question.answers %>
<%#raise a.inspect%>
<% a.each do |sushil| %>
<%#raise sushil.inspect%>
<% if sushil.correct_answer?%>
<%count = count+1 %>
<%else %>
<%count = count+0 %>
<%end%>
<%end%>
<%#raise count.inspect%>
<%if count == 1 %>
<% for answer in question.answers %>
<%= radio_button_tag("result[question_id][#{question.id}]", answer.id ) %>
<%= answer.content %><br>
<%end%>
<%elsif count >= 2 %>
<% for answer in question.answers %>
<%= check_box_tag("result[question_ids][][#{question.id}]", answer.id ) %>
<%#= check_box_tag ("result[question_id][#{question.id}]", answer.id ) %>
<%= answer.content %><br>
<% params[:answer_id] = answer.id %>
<%end%>
<% end %>
<%# raise params[:answer_id].inspect%>
<% end %>
</div>
<div class="form-actions">
<center><%= f.submit "Submit", :class => 'btn btn-primary',:onclick => "if(confirm('Are you sure you want to Submit the Paper?')) return true; else return false;" %></center>
</div>
<% end %>
</div>
</div>
</div>
<div style='display:none' id="timeup"><% if #time==0 %>0<%else%>1<%end%></div>
<!-- Added javascript for back button-->
<script>
jQuery(document).ready(function($) {
if (window.history && window.history.pushState) {
window.history.pushState('forward', null, './#forward');
$(window).on('popstate', function() {
history.forward();
});
}
});
</script>
<!-- Added Timer Javascript in Test -->
<% if #paper.timing!=0 %>
<script>
$(document).ready(function(){
var now=new Date();
if($('#timeup').html()==0){
stopTest();
}
now.setMinutes(now.getMinutes()+<%=#min%>);
now.setSeconds(now.getSeconds()-<%=#sec%>);
$('#time').countdown({until:now, format: 'MS',onExpiry:stopTest});
});
function stopTest(){
$('#time').html('<center><h4>Time\'s up!</h4></center>');
$('#timeup').html('0');
// $('input.radio').attr("onclick","return false;");
$("#new_result").submit();
}
</script>
<%end%>
You should use kaminari or will_paginate gem...
Kaminari example:
Gemfile:
gem 'kaminari'
bash:
bundle
rails g kaminari:views default
products_controller.rb:
#products = Product.order("name").page(params[:page]).per(5)
config/locales/en.yml:
en:
hello: "Hello world"
views:
pagination:
previous: "< Previous"
next: "Next >"
truncate: "..."
products/index.html.erb:
<%= paginate #products %>
app/views/kaminari/_prev_span.html.erb
<span class="prev disabled"><%= raw(t 'views.pagination.previous') %></span>
Will_paginate example:
http://richonrails.com/articles/getting-started-with-will-paginate

Render partial with locals and html tags

I've created a partial which I'm rendering in my view. I'm using two locals: icon and path.
The issue I'm encountering is that I don't know how to pass the following parameters:
"About", "https://www.google.com", target: "_blank", class: "medium button radius", id: "about_us"
to the path local.
Any help/tips, greatly appreciated.
Code without locals
_footer.html.erb
<div class="footer">
<div class="row">
<div class="small-12 columns">
<div class="footer-icon">
<span><%= fa_icon “home” %></span>
</div>
<div class="footer-button">
<%= link_to "About", "https://www.google.com",
target: "_blank", class: "medium button radius",
id: "about_us" %>
</div>
</div>
</div>
</div>
about.html.erb
<% content_for :footer_class do %>
<% render partial: 'layouts/footer', %>
<% end %>
Code with locals
_footer.html.erb
<div class="footer">
<div class="row">
<div class="small-12 columns">
<div class="footer-icon">
<span><%= fa_icon icon %></span>
</div>
<div class="footer-button">
<%= link_to path %>
</div>
</div>
</div>
</div>
about.html.erb
<% content_for :footer_class do %>
<% render partial: 'layouts/footer', locals: {icon: "home", path: ? } %>
<% end %>
You could build a hash and access the values in the view:
about.html.erb
<% path_hash = {
label: "About",
destinatoin: "https://www.google.com",
target: "_blank",
class: "medium button radius",
id: "about_us"
} %>
<% content_for :footer_class do %>
<% render partial: 'layouts/footer', locals: {icon: "home", path_attrs: path_hash } %>
<% end %>
Then in _footer.html.erb
<div class="footer-button">
<%= link_to path_attrs[:label], path_attrs[:destination], target: path_attrs[:target], id: path_attrs[:id], class: path_attrs[:class] %>
</div>
Not sure why you're doing it this way in the first place. Seems like it could get messy over time, but this should do the trick for you.

Rendering a collection isn't rendering all items in the collection?

This seems very elementary, so I'm not sure what I'm doing wrong. I have a #videos collection (which debugging #videos.count reveals to be 4), but the line <%= render #videos %> is only rendering 2 of the 4 items. Here is the controller method (VideosController):
def index
#video = Video.new(athlete_id: current_user.id, sport_id: current_athlete_sport.id)
#videos = current_user.videos_for_sport(current_athlete_sport).order("date DESC")
debugger
respond_with #videos
end
And the _video partial that the aforementioned <%= render #videos %> line is rendering:
<%= form_for video do |f| %>
<div class="row edit-video-container <%= video == #videos.pop ? 'last' : '' %>">
<div class="span4">
<div class="row">
<div class="span4 video-thumbnail">
<%= image_tag video.thumbnail_url || asset_path("video-encoding-placeholder.png"), {alt: "", title: "#{video.name || "My video" + video.id}"} %>
<div class="video-ribbon">
<ul>
<li class="pull-left">
<%= link_to "#video-player-modal", { data: { toggle: "modal", link: video.mp4_video_url, thumbnail: video.thumbnail_url }, role: "button", class: "video-player-link" } do %>
<i class="icon video-play"></i>
<p>Play</br>Video</p>
<% end %>
</li>
<li class="pull-left">
<%= link_to rotate_video_path(video, direction: "ccw"), :data => { :method => "put", :confirm => "Are you sure?", :type => 'json' } do %>
<i class="icon video-rotate-left"></i>
<p>Rotate</br>Left</p>
<% end %>
</li>
<li class="pull-left">
<%= link_to rotate_video_path(video, direction: "cw"), :data => { :method => "put", :confirm => "Are you sure?", :type => 'json' } do %>
<i class="icon video-rotate-right"></i>
<p>Rotate</br>Right</p>
<% end %>
</li>
<li class="pull-left">
<%= link_to video_path(video), :data => { :method => "delete", :confirm => "Are you sure?", :type => 'json' } do %>
<i class="icon video-delete"></i>
<p>Delete</br>Video</p>
<% end %>
</li>
</ul>
</div>
</div>
</div>
<div class="row">
<div class="span4">
<%= f.check_box :featured, { checked: video.featured?, class: "autosave checkbox-right", data: { event: 'change' } } %>
<%= label_tag "video-checkbox-featured-#{video.id}", "Use as featured video?", { class: "checkbox-right-label" } %>
</div>
</div>
</div>
<div class="span4">
<div class="row">
<div class="span4">
<%= label :video, :name, "Video Name" %>
<%= f.text_field :name, { class: "span4 autosave"} %>
</div>
</div>
<div class="row">
<div class="span4">
<%= label :video, :video_type_id, "Video Type" %>
<%= f.select:video_type_id, VideoType.all.collect { |vid| [vid.name, vid.id] }, { include_blank: "Choose One", selected: video.video_type_id }, { class: "chosen-select autosave", id: "", data: { event: 'change' } } %>
</div>
</div>
<div class="row">
<div class="span4">
<%= label :video, :sport_id, "Video Sport" %>
<%= f.select :sport_id, current_user.sports.collect { |sp| [sp.name, sp.id] }, { include_blank: "Choose one", selected: video.sport_id }, { class: "chosen-select autosave", id: "", data: { event: "change" } } %>
</div>
</div>
<div class="row">
<div class="span4">
<%= label :video, :date, "Date Recorded" %>
<%= f.text_field :date, {class: "autosave date datePicker span4", value: js_date(video.date) } %>
</div>
</div>
<div class="row">
<div class="span4">
<%= label :video, :uniform_number, "Uniform Number" %>
<%= f.text_field :uniform_number, { class: "autosave span4"} %>
</div>
</div>
</div>
</div>
<% end %>
Again, throwing a debugger in the controller, or a debug on the page just before the <%= render #videos %> line shows 4 videos, however only 2 are actually being rendered. At a bit of a loss here!
The problem is here:
video == #videos.pop
Pop takes out a member from your collection. You shouldn't use it here because you brake the looping.
If this is the case, I suppose you can see only the first and the last item of your collection.

Resources