I have been following this tutorial to get the leaderboard gem working and I've got all of the leaderboard gem aspect of it working, but I am struggling to get the Kaminari gem part of it working (the paginate gem).
At the moment in my controller I have this:
class LeaderboardController < ApplicationController
before_action :query_options
def show
#lb = Boards.default_leaderboard
#entries = entry_service.execute(query_options)
respond_to do |format|
format.html do
paginate
end
format.json do
render json: #entries
end
end
end
private
def query_options
#limit = [params.fetch(:limit, 10).to_i, 100].min
#page = params.fetch(:page, 1).to_i
{ page: #page, limit: #limit }
end
def paginate
pager = Kaminari.paginate_array(
#entries,
total_count: #lb.total_members)
#page_array = pager.page(#page).per(#limit)
end
And in the views I have:
= paginate #page_array
But for some reason this is not paginating the #entries...
For the way you have it set up you need to call the before_action :paginate.
Related
For instance, I have got a Comapny model. I can get per page limit of 10 in the way as below?
class Client::CompanyController < ApplicationController
def company_search
#companies = Company.all.page(params[:page],:per_page=>10)
end
end
You can user will_paginate gem
and than in your controller You can put:
def company_search
#companies = Company.all.paginate(page: params[:page], per_page: 10)
end
and in Your view:
# render your companies data with render or #companies.each do |company|
render #companies
# pagination
will_paginate #companies
I am receiving this error:
ActiveRecord::RecordNotFound in MainController#compare Couldn't find
Post without an ID
Here is my code for main controller:
#job_one = Post.find(params[:j1])
#job_two = Post.find(params[:j2])
UPDATE #1:
main_controller.rb
class MainController < ApplicationController
skip_before_action :authenticate_user!
def index
#posts = Post.all.order(id: :asc)
end
def dummy
end
def editor
end
def compare
#job_one = Post.find(params[:j1])
#job_two = Post.find(params[:j2])
respond_to do |format|
format.html
format.pdf do
pdf = Prawn::Document.new
pdf.text "Hello World"
send_data pdf.render
end
end
end
end
What I am doing is comparing two posts that are made side by side. So these are the two posts I chose from the index page.
Can anyone off some assistance?
How to extend a controller method from a Rails Engine without having to duplicate the whole thing?
Trying to extend https://github.com/radar/forem/blob/rails4/app/controllers/forem/forums_controller.rb -- app/decorators/controllers/forem/forums_controller_decorator.rb:
Ideal
Forem::ForumsController.class_eval do
def show
# A simple `include` here or something?
# New code goes here...
end
end
Current
Forem::ForumsController.class_eval do
def show
# Repeat ALL the code from:
# https://github.com/radar/forem/blob/rails4/app/controllers/forem/forums_controller.rb
authorize! :show, #forum
register_view
#topics = if forem_admin_or_moderator?(#forum)
#forum.topics
else
#forum.topics.visible.approved_or_pending_review_for(forem_user)
end
#topics = #topics.by_pinned_or_most_recent_post
# Kaminari allows to configure the method and param used
#topics = #topics.send(pagination_method, params[pagination_param]).per(Forem.per_page)
respond_to do |format|
format.html
format.atom { render :layout => false }
end
# New code goes here...
end
end
We use this gem for multiple applications and engines to do exactly what you want:
https://github.com/EPI-USE-Labs/activesupport-decorators
I could extend a controller method from a Rails Engine without having to duplicate code using alias_method
module ValueSets
SetsController.class_eval do
def new_with_authorize
new_without_authorize
authorize #value_set
end
alias_method :new_without_authorize, :new
alias_method :new, :new_with_authorize
end
end
I'm fairly new to Rails and I have a Ruby on Rails 3.2 application and I've integrated the Youtube_it gem seen here https://github.com/kylejginavan/youtube_it. The gem works fine and I'm able to upload the video to youtube, but it takes a while to process the video. I would like to be able to run that as a background job and redirect the user to the thank you page I have created.
I'm not sure where to call the delay method. I would like to call the delay method and then have the user redirect to the page_path('thank-you') page.
Any help would be greatly appreciated. I've searched all over for an answer.
VideosController
def upload
#video = Video.create(params[:video])
if #video
#upload_info = Video.delay.token_form(params[:video], save_video_new_video_url(video_id: #video.id))
else
respond_to do |format|
format.html { render "/videos/new" }
end
end
end
def save_video
#video = Video.find(params[:video_id])
if params[:status].to_i == 200
#video.update_attributes(youtube_id: params[:id].to_s, is_complete: true, user_id: current_user.id, approved: false)
Video.delete_incomplete_videos
else
Video.delete_video(#video)
end
#redirect_to videos_path, notice: "video successfully uploaded"
redirect_to page_path('thank-you')
end
Here is my controller.
class VideosController < ApplicationController
before_filter :authenticate_user!, only: [:new, :upload, :save_video, :destroy]
def index
if params[:category]
Video.yt_session
#videos = Video.approved.where(category_id: params[:category])
else
Video.yt_session
#videos = Video.approved
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #videos }
end
end
def show
#video = Video.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #video }
end
end
def new
#video =Video.new
#categories = Category.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: #video }
end
end
def upload
#video = Video.create(params[:video])
if #video
#upload_info = Video.token_form(params[:video], save_video_new_video_url(video_id: #video.id))
else
respond_to do |format|
format.html { render "/videos/new" }
end
end
end
def save_video
#video = Video.find(params[:video_id])
respond_to do |format|
if #video.update_attributes(:youtube_id => params[:id].to_s, :is_complete => true,:user_id=>current_user.id,:approved=>false)
format.html { redirect_to page_path('thank-you') }
format.json { head :no_content }
else
format.html { Video.delete_video(#video) }
format.json { render json: #category.errors, status: :unprocessable_entity }
end
end
end
def destroy
#video = Video.find(params[:id])
if Video.delete_video(#video)
flash[:notice] = "Video deleted."
else
flash[:error] = "We were unable to delete this video."
end
redirect_to videos_path
end
def vote_up
#video = Video.find(params[:id])
#video.update_attribute(:votes_up, (#video.votes_up.to_i + 1))
redirect_to #video
end
protected
def collection
#videos ||= end_of_association_chain.completes
end
end
You can always use background workers to do that using the like of delayed_job, but personally speaking I prefer to use Resque/beanstalkd/RabbitMQ since I can run multiple workers, concurrently.
To make your life even easier, you just try Sidekiq (https://github.com/mperham/sidekiq).
You will move your worker logic to some worker.
Include Sidekiq to your Gemfile
gem 'sidekiq'
At your controller add something like:
VideoSaverWorker.perform_async(#video.id)
The VideoSaverWorker must look something like:
class VideoSaverWorker
include Sidekiq::Worker
sidekiq_options queue: "high"
def perform(video_id)
video = Video.find(video_id)
.....
.....
end
end
Please note using this you will do the work at a background thread, but it wont redirect you to the related page.
You will need to do some workarounds, something like periodically refrishing the page till you see the changes at your view html page, or maybe you can push the changes to your webpage using Faye or Node.js.
I am attempting to display a comments feed for my application but I keep running into the same error. In my controller I have:
respond_to do |format|
format.rss { render :layout => false }
end
I then have a index.rss.builder w/ something very similar to http://techgossipz.blogspot.com/2010/03/gnerate-rss-for-your-site-in-rails.html
I am using Rails 3. Is there something that I am doing wrong?
I've done it like this:
class RssController < ApplicationController
respond_to :xml, :html, :rss
layout false
after_filter :set_header
def set_header
response.headers['Content-Type'] = 'application/xml; charset=utf-8'
end
def hu
#feeds=Feed.order("created_at DESC")
respond_with(#feeds)
end
def en
#feeds=Feed.order("created_at DESC")
respond_with(#feeds)
end
end
And you can call /en.rss