Rails 4 - Displaying a Users Search Results to a View - ruby-on-rails

I'm using the Spotify API and am trying display search results for an artist on the same view page that the search form is on. I think I'm close but just need a little bit of help. Rails noob obviously..
How do I set the url http://localhost:3000/search?query=what_do_i_set_this_to??? to the user input I'm trying to collect via this line in my search form <%= text_field_tag(:query, "", class: 'form-control', autofocus: true, placeholder: "Enter artist name") %>
Much thanks in advance! I've been stuck on this for about a week now.
UPDATE (what my code currently looks like now)
Controller
artists_controller.rb
class ArtistsController < ApplicationController
def index
request = search_params[:query]
unless request.nil?
response = HTTParty.get("https://api.spotify.com/v1/search?q="+request+"&type=artist")
#hash_version = JSON.parse(response.body)
end
end
private
def search_params
params.permit(:query, :utf8, :commit)
end
end
View
artists/index.html.erb
<h1>Artist Search</h1>
<i>(search for artists you would like to follow)</i>
<br>
<br>
<%= form_tag(artists_path, method: :get) do %>
<div class="form-group">
<%= label_tag(:query, "Search artist by name:") %>
<%= text_field_tag(:query, params[:query], class: 'form-control', autofocus: true, placeholder: "Enter artist name") %>
</div>
<div class="form-group">
<%= submit_tag("Search", class: 'btn btn-success') %>
</div>
<% end %>
<% #hash_version["artists"]["items"].each_with_index do |band, index| %>
<% if index == #hash_version["artists"]["items"].size - 1 %>
<li class="artist_li">
<%= image_tag(band["images"].first["url"], class: 'img-responsive artist_img') rescue image_tag("microphone.png", class: 'img-responsive artist_img mic_bg') %>
<br>
<h1 class="artist_name"><%= band["name"] %></h1>
<br>
<% if band["genres"].empty? %>
<h5>no genres</h5>
<% else %>
<h5><%= band["genres"] %></h5>
<% end %>
<br>
<h5><%= band["followers"]["total"] %> followers</h5>
<br>
<h4>(Follow)</h4>
<!-- no hr with last li -->
</li>
<% else %>
<li class="artist_li">
<%= image_tag(band["images"].first["url"], class: 'img-responsive artist_img') rescue image_tag("microphone.png", class: 'img-responsive artist_img mic_bg') %>
<br>
<h1 class="artist_name"><%= band["name"] %></h1>
<br>
<% if band["genres"].empty? %>
<h5>no genres</h5>
<% else %>
<h5><%= band["genres"] %></h5>
<% end %>
<br>
<h5><%= band["followers"]["total"] %> followers</h5>
<br>
<h4>(Follow)</h4>
<hr class="artists_hr">
</li>
<% end %>
<% end %>

You should us the name option and passing it in the value option as params[:query] as follow :
<h1>Artist Search</h1>
<i>(search for artists you would like to follow)</i>
<br>
<br>
<%= form_tag(artists_path, method: :get) do %>
<div class="form-group">
<%= label_tag(:query, "Search artist by name:") %>
<%= text_field_tag(:query, params[:query], class: 'form-control', autofocus: true, placeholder: "Enter artist name") %>
</div>
<div class="form-group">
<%= submit_tag("Search", class: 'btn btn-success') %>
</div>
<% end %>
<% unless #hash_version.nil? %>
<% #hash_version["artists"]["items"].each_with_index do |band, index| %>
<% if index == #hash_version["artists"]["items"].size - 1 %>
<li class="artist_li">
<%= image_tag(band["images"].first["url"], class: 'img-responsive artist_img') rescue image_tag("microphone.png", class: 'img-responsive artist_img mic_bg') %>
<br>
<h1 class="artist_name"><%= band["name"] %></h1>
<br>
<% if band["genres"].empty? %>
<h5>no genres</h5>
<% else %>
<h5><%= band["genres"] %></h5>
<% end %>
<br>
<h5><%= band["followers"]["total"] %> followers</h5>
<br>
<h4>(Follow)</h4>
<!-- no hr with last li -->
</li>
<% else %>
<li class="artist_li">
<%= image_tag(band["images"].first["url"], class: 'img-responsive artist_img') rescue image_tag("microphone.png", class: 'img-responsive artist_img mic_bg') %>
<br>
<h1 class="artist_name"><%= band["name"] %></h1>
<br>
<% if band["genres"].empty? %>
<h5>no genres</h5>
<% else %>
<h5><%= band["genres"] %></h5>
<% end %>
<br>
<h5><%= band["followers"]["total"] %> followers</h5>
<br>
<h4>(Follow)</h4>
<hr class="artists_hr">
</li>
<% end %>
<% end %>
<% end %>
then in your controller just look for params[:query] and verify that it has some data before sending the request as follow :
class ArtistsController < ApplicationController
def index
request = search_params[:query]
unless request.nil?
response = HTTParty.get("https://api.spotify.com/v1/search?q="+request+"&type=artist")
#hash_version = JSON.parse(response.body)
end
end
private
def search_params
params.permit(:query, :utf8, :commit)
end
end
and you should be good :)

Related

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.

Rails - Search results are not filtered. Returning all availability

The task scope: A user selects an option from a drop down menu (Single/Double bedroom), clicks on search button and gets all related results. Then on the search page the user can further refine results by using available filters (TV/Shower). For some reason none of these actions modify the search results- the page displays all available listings in the database, rather than the ones that match the criteria..
What am I doing wrong?
Here is what I have so far:
HOME SEARCH BAR
<%= form_tag search_path, method: :get do %>
<div class="row">
<div class="col-md-7">
<%= select_tag :bedroom, options_for_select([['Single', 1], ['Double', 2]]), class: "form-control" %>
</div>
<div class="col-md-2">
<%= submit_tag "Search", class: "btn btn-normal btn-block" %>
</div>
</div>
<% end %>
SEARCH PAGE
<div class="col-sm-3">
<%= search_form_for #search, url: search_path, remote: true do |f| %>
<div class="row">
<div>
<%= check_box_tag "q[is_tv_eq]", true %> TV
</div>
<div>
<%= check_box_tag "q[is_shower_eq]", true %> Shower
</div>
</div>
<div class="row text-center">
<%= f.submit "Search", class: "btn btn-form" %>
</div>
<% end %>
<%= render partial: "rooms/rooms_list", locals: {rooms: #arrRooms} %>
</div>
ROOM LIST PARTIAL
<% rooms.each do |room| %>
<div class="row">
<%= image_tag room.cover_photo(:medium) %>
<%= link_to room.user_id, room %>
<%= room.price %> - <%= room.bedroom %>
<div id="star_<%= room.id %>"></div> <%= pluralize(room.average_rating, "review") %>
</div>
<script>
$('#star_<%= room.id %>').raty({
path: '/assets',
readOnly: true,
score: <%= room.average_rating %>
});
</script>
<% end %>
ROOM MODULE
class CreateRooms < ActiveRecord::Migration[5.0]
def change
create_table :rooms do |t|
t.string :bedroom
t.integer :price
t.boolean :active
t.timestamps
end
end
end
PAGE CONTROLLER
def search
# STEP 1
if params[:search].present? && params[:search].strip != ""
session[:loc_search] = params[:search]
end
# STEP 2
if session[:loc_search] && session[:loc_search] != ""
#rooms_bedroom = Room.where(active: true, bedroom: session[:loc_search]).order(:price)
else
#rooms_bedroom = Room.where(active: true).all
end
# STEP 3
#search = #rooms_bedroom.ransack(params[:q])
#rooms = #search.result
#arRoooms = #rooms.to_a
end
DEFINED THE BEDROOM OPTIONS IN THE ROOM OVERVIEW PAGE (not sure if that helps)
<%= form_for #room do |f| %>
<div class="form-group">
<label> Bedroom Type </label>
<%= f.select :bedroom, [["Single", "Single"], ["Double", "Double"]],
id: "bedroom", prompt: "Select...", class: "form-control" %>
</div>
</div>
<div><%= f.submit "Save", class: "btn btn-normal" %></div>
<% end %>
Thanks

acts-as-taggable-on gem is not persisting tag strings into edit or into show

I have tags on my video objects. I can submit tags when creating a new video via my form however they don't appear on my video show page, video index or video edit form, which makes me think they are not being presisted to the database but I'm unclear on what I as missing or doing wrong.
models/video.rb
class Video < ActiveRecord::Base
after_create { validates_presence_of :user, :post }
belongs_to :user
belongs_to :post
has_many :comments
validates :title, presence: true
acts_as_taggable
end
views/videos/index.html.erb
<%= will_paginate #videos %>
<div class="" id="tag_cloud">
<% tag_cloud Video.tag_counts.sort { |x, y| x.name <=> y.name }, %w[s m l] do |tag, css_class| %>
<%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>
</div>
<div class="">
<% #videos.each do |video| %>
<div class="">
<%= link_to video.title, video_path(video) %></br >
<%= content_tag(:iframe, nil, src: "//www.youtube.com/embed/#{video.embed_id}") %>
<p>Created by: <%= video.user.username %>, at: <%= video.created_at.strftime("%e, %b, %Y, %H:%M:%S %p") %></p>
<% unless video.comments.last.nil? %>
<%= video.comments.last.body %>
<% end %>
<p>
Tags: <%= raw video.tag_list.map { |t| link_to t, tag_path(t) }.join(", ") %>
</p>
<% end %>
</div>
</div>
views/videos/_new.html.erb
<div class="">
<% if current_user == #post.user || current_user.admin %>
<h3>Add video</h3>
<div class="">
<%= form_for [#post, #video] do |f| %>
<div class="">
<%= f.label(:title, "Video Title") %></br >
<%= f.text_field(:title) %>
</div>
<div class="">
<%= f.label(:url, "Video Url") %></br >
<%= f.text_field(:url) %>
</div>
<div class="">
<%= f.label(:tag_list, "Tags (separated by commas)") %></br >
<%= f.text_field(:tag_list, multiple: true) %>
</div>
<div class="">
<%= f.submit("Submit Video") %>
</div>
<% end %>
</div>
<% end %>
</div>
views/videos/show.html.erb
<div class="video-show">
<%= #video.title %>
<%= content_tag(:iframe, nil, src: "//www.youtube.com/embed/#{#video.embed_id}") %>
<p>
Tags: <%= #video.tag_list %>
</p>
<% if current_user == #video.user %>
<%= link_to "Edit Video", edit_video_path(#video) %> |
<%= link_to "Delete Video", video_path(#video), method: :delete, data: { confirm: "Are you sure?" } %> |
<% end %>
<% if current_user == #video.user %>
<%= link_to("Back to Log", post_path(#post)) %> |
<% end %>
<%= link_to("Back to Index", videos_path) %>
</div>
controllers/videos_controller.rb
def index
if params[:tag]
#videos = Video.tagged_with(params[:tag]).page(params[:page]).per_page(10).order(created_at: "desc")
else
#videos = Video.all.page(params[:page]).per_page(10).order(created_at: "desc")
end
end
private
def video_params
params.require(:video).permit(
:title,
:url,
:tag_list,
)
end

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

if !checklogin? then return end

I would like to make show.html.erb(groups) pages open to visitors(,which are not users) as well.
But my code has "if !checklogin? then return end" on groups_controller, so they can't see the show.html.erb pages. I know I should remove "if ! checklogin? then return end" but have no idea how to reconstruct my code as a whole.
Could you give me some hints?
☆members_controller
def checklogin?
if session[:user_id] != nil then
return true
else
redirect_to '/members/login'
return false
end
end
☆login.html.erb(members_controller)
<div class="span4">
<h4 class="title">Users' reading texts</h4>
<% #books.each do |book| %>
<%= image_tag book.imageurl, :width => '30px', :height => '30px'%>
<% end %>
</div><!--span4-->
☆groups_controller
def show
#group = Group.find(params[:id])
#group_message = Group.find(params[:id]).group_messages.build
if !checklogin? then return end
#group_messages = Group.find(params[:id]).group_messages.order("created_at desc")
#me = me?
#member = Member.find(session[:user_id])
#isGr = GroupInMember.where(:member_id => session[:user_id], :group_id =>
params[:id].to_i).count > 0
#gms = Group.find(params[:id]).group_messages.order("created_at desc").scoped
if params[:page].present?
#gms = #gms.where("page = ?" , params[:page] )
end
if params[:content].present?
#gms = #gms.where("content like ?" , "%" + params[:content] + "%")
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #group_messages }
end
end
☆show.html.erb(groups)
<div class="container-fluid">
<div class="row-fluid">
<div class="span4">
<div id="individual">
<h4>※「<%= #group.name %>」のページ</h4>
<%= link_to image_tag(#group.imageurl, :width=>"100" ,:height=>"150"),#group.detailurl , :target => '_blank' %>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>プロフィール</th>
</tr>
</thead>
<tbody>
<tr>
<th>著者</th>
<td><%= #group.author %></td>
</tr>
<tr>
<th>出版年</th>
<td><%= #group.published %></td>
</tr>
<tr>
<th>出版社</th>
<td><%= #group.publish %></td>
</tr>
<tr>
<th>ページ数</th>
<td><%= #group.page %></td>
</tr>
<tr>
<th>ISBN</th>
<td><%= #group.isbn %></td>
</tr>
</tbody>
</table>
</div><!--individual-->
<%# if #group.admin %>
<%#= link_to 'Profile Edit', edit_group_path(#group),class: "btn btn-midium btn-primary" %>
<%# end %>
<hr>
<% if session[:user_id]%>
<div class="group-side">
<% if #isGr %>
※登録済みです。
<% else %>
※登録していません。
<% end %>
</div>
<p class="group-side">
<%= link_to '本棚登録/解除', {:controller => 'groups', :action => 'join', :id =>
#group.id }, class: "btn btn-midium btn-primary"%></p>
</p>
<p></p>
<div class="group-side2"><b>この本を登録した人一覧:</b></div>
<% #group.group_in_members.each do |m| %>
<% #member = Member.find(m.member_id) %>
<% if #member.provider %>
<%= image_tag #member.image, :width =>'30px', :height =>'30px' %>
<% elsif #member.avatar_file_name %>
<%= image_tag #member.avatar.url(:thumb), :width =>'30px', :height => '30px' %>
<% else %>
<%= image_tag 'love.png', :width =>'30px', :height => '30px' %>
<% end %>
<%# if GroupInMember.where( :member_id => session[:user_id], :group_id => #group.id ).length > 0 %>
<%# end %>
<% end %>
</div>
<% end %>
<div class="span8">
<p>
<b></b>
</p>
<div class="post_on_group">
<% if GroupInMember.where(:member_id =>session[:user_id], :group_id=>#group.id).length > 0 %>
<%= form_for([#group, #group_message]) do |f| %>
<% if #group_message.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#group_message.errors.count, "error") %> prohibited this group_message from being saved:</h2>
<ul>
<% #group_message.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%#= f.label :member_id %>
<%= f.hidden_field :member_id, :value => session[:user_id] %>
</div>
<div class="field">
<%#= f.label :group_id %>
<%= f.hidden_field :group_id %>
</div>
<div class="form_index2">
<%#= f.label :"" %>
<%= f.text_field :page, :class=> "span2" %>ページ(※半角数字の入力必須)
</div>
<div class="form_index4">
<%#= f.label :"何行目あたり?" %>
<%= f.text_field :line, :class=> "span1" %>行目あたり(※半角数字。入力は任意)
</div>
<div class="form_index4">
<%= f.label :"投稿内容(※必須)" %>
<%= f.text_area :content ,:class => "span12",:size => "20x5" %>
</div>
<div class="actions">
<%= f.submit "投稿"%>
</div>
<% end %>
<% end %>
</div> <!--post_on_group-->
<div>
※この本に関する投稿をページ数や投稿内容から検索できます。
</div>
<div class= "form_index">
<%= form_tag({:action=>"show"}, {:method=>"get"}) do %>
<div class="from_field_index5">
<%= text_field_tag 'page' ,'', :class=> "span3" %>
<%= submit_tag 'ページ' %>
<% end %>
</div>
</div>
<div class= "form">
<%= form_tag({:action=>"show"}, {:method=>"get"}) do %>
<div class="from_field_index">
<%= text_field_tag 'content' %>
<%= submit_tag '投稿内容' %>
<% end %>
</div>
</div>
<hr>
<br/>
<% if #gms %>
<% #gms.each do |gm| %>
<div class="message_area">
<div class="each_message">
<%= image_tag gm.group.imageurl,:width => '20', :height => '25' %>
<%= 'Page:' + gm.page.to_s + '&' %><%= 'Line:' + gm.line.to_s %>
<%= gm.member.name %>
(<%= gm.created_at.strftime'%Y-%m-%d %H:%M' %>)
<div class="group_message">
<p class="message_content"><a href="/group_messages/<%= gm.id%>" > <%= truncate(gm.content, { :length => 300}) %></a></p>
</div><!--group_message-->
<br/>
<% if gm.group_message_comments.present? %>
<% gm.group_message_comments.order("created_at asc").each do |gmsc|%>
<div class="group_message_comment">
<p><%= gmsc.member ? gmsc.member.name : "unknown" %> (<%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %>)</p>
<%= truncate(gmsc.content, { :length => 300}) %>
</div> <!--group_message_comment-->
<br/>
<% end %><!-- each do -- >
<% else %>
<% end %> <!--if -->
</div><!--each_message-->
<br>
</div> <!--message_area-->
<% end %>
<% else %>
<% #group_messages.each do |gm| %>
<div class="message_area">
<div class="each_message">
<%= image_tag gm.group.imageurl,:width => '20', :height => '25' %>
<%= 'Page:' + gm.page.to_s + '&' %><%= 'Line:' + gm.line.to_s %>
<%= gm.member.name %>
(<%= gm.created_at.strftime'%Y-%m-%d %H:%M' %>)
<div class="group_message">
<p class="message_content"><a href="/group_messages/<%= gm.id%>" > <%= truncate(gm.content, { :length => 300}) %></a></p>
</div><!--group_message-->
<br/>
<% if gm.group_message_comments.present? %>
<% gm.group_message_comments.each do |gmsc|%>
<div class="group_message_comment">
<p><%= gmsc.member ? gmsc.member.name : "unknown" %> (<%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %>)</p>
<%= truncate(gmsc.content, {:length =>300}) %>
</div> <!--group_message_comment-->
<br/>
<% end %><!-- each do -- >
<% else %>
<% end %> <!--if -->
</div><!--each_message-->
<br>
</div> <!--message_area-->
<% end %> <!--each do -->
<% end %>
</div><!--span8-->
The way to check login and redirect, is by using the callback of before_filter
before_filter :check_login
check_login will redirect user to login action unless redirect
def check_login
unless session[:user_id]
flash[:error] = "You must be logged in to access this section"
redirect_to '/members/login'
end
end
If you want to enable access just for some actions - you can exclude them:
before_filter :check_login, :except => [:index, :show, etc..]

Resources