Paginate child models Kaminari on rails 3.1 - ruby-on-rails

I have 2 models images and categories.
The relations are:
- a image belongs to one category.
- a category has many images.
On the index.html.erb view from categories object, I want paginate 20 images per page for all categories.
I mean, I want paginate all images from all categories with 20 images per page.
in my index action on categories_controller.rb method I have:
def index
#categories = Category.all
#categories.each do |category|
#images = Kaminari.paginate_array(category.images).page(1).per(1)
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #categories }
end
end
on index.html.erb I have:
<% #images.each do |image|%>
code for each image here
<% end %>
<% paginate #images %>
But this for me it does not works. I can not see anything image showed.
how I can implement this functionality? and fix this problem? Thank you.

#categories.each do |category|
#images = Kaminari.paginate_array(category.images).page(1).per(1)
end
In this loop, you're overriding #images each time, so obviously, you wont get what you expect ;)
I think that what you wanna do is:
#images = Image.where("category_id IS NOT NULL").page(params[:page]).per(20)
The .where("category_id IS NOT NULL") is not necessary if images have to belong to a category.

Related

Render collection [where(store: current_user).each do] ? how to load specific data from render

Sorry, this may seem simple.
I'm using 'render #orders' but I need only the orders where that have the Storeid = current_user
I'd normally do
Order.where(Storeid: current_user).each do |order|
ect ect
But I'm unsure how to do this with render.
Filter the #orders instance variable in your controller:
class OrdersController < ApplicationController
def index
#orders = Order.where(store_id: current_user.id)
end
end
Then in your view app/views/orders/index.html.erb:
<%= render #orders %>

Render different show pages with category in ruby on rails

I need render different show pages for my blog posts.
I have 3 different categories : themes, snippets, projects.
Each blog post must related with those three categories.
If anyone click post(assume related category is snippets), it display in different show...etc... It is same for other categories.
How it is possible with conditional statements.
you can make routes like:
resources :posts, except:[:show]
get 'posts/:id/cat/:category' , to:'posts#show', as: :show
you have to create partial for categories as follows:
app/views/posts/_themes.html.erb
app/views/posts/_snippets.html.erb
app/views/posts/_projects.html.erb
then in controller's show action.
controllers/posts_controller.rb
def show
#post = Post.find(params[:id])
#category = params[:category]
...
end
Then render that category in show page.
views/posts/show.html.erb
...
<%= render '#{#category}'%>
Just one show method and you can render different views conditionally and simple render works for you, you can use below code:
Just render with HTML file name if file is in same controller's view
if #post.theme?
render 'themes'
elsif #post.snippet?
render 'snippets'
else
render 'projects'
end
I would start with this, then refactor for avoiding repetition
categories_controller.rb
def themes
##posts = Post.where(category_id: 1)
...
render layout: themes
end
def snippets
##posts = Post.where(category_id: 2)
...
render layout: snippets
end
def projects
##posts = Post.where(category_id: 3)
...
render layout: snippets
end
You can do something like this:
models/post.rb
def category
#category ||= ... #returns category name
end
controllers/post_controller.rb
def show
#post = Post.find(id)
#category = #post.category
...
end
views/posts/show.html.erb
...
<%= render "types/#{#category}" %>
...
Also you can render specified template from controller if you don't have any common parts for categories
You can use rails partial to achieve this:
to know more about partial refer Rails Partial
you can create partial for categories as follows:
app/views/categories/_themes.html.erb
app/views/categories/_snippets.html.erb
app/views/categories/_projects.html.erb
in show page of categories you can modify the show page as follows
class CategoriesController < ApplicationController
...
def show
...
render params[:category]
end
...
end
Now when you call show page pass a param category which denotes the type of category which you want to show
if you want to show category snippets then the params will be params[:category] = 'snippets' this will look for partial inside categories view.

Rails Two Views With One Controller#Action

I created a rails association "user has many follows" currently have lists of user's followers and followings. I want to have 2 links onusers#show to display lists of followers and followings. I know I can render two different views in my follows#index using if else statement but I'm not sure how to go about it. My initial thought was to have rails check which link was clicked (either 'Followers' or 'Followings') but I couldn't find an exact answer. I also thought of creating two different tables (Followers, Followings) but adding 2 same values for 1 action seemed redundant and waste of space.
Here is my FollowsController:
class FollowsController < ApplicationController
def index
#follows = Follow.all
render :index
end
def create
#follow = Follow.create(follow_params)
redirect_to user_url(#follow.user_id)
end
def destroy
#follow = Follow.find(params[:id])
#follower_id = #follow.user_id
#follow.destroy!
redirect_to user_url(#follower_id)
end
private
def follow_params
params.require(:follow).permit(:follower_id, :user_id)
end
end
What would be the best approach?
Example:
def index
#follows = Follow.all
if #follows.present?
render 'show_with_followers'
else
render 'show_without_followings'
end
end
views
app/views/follows/_show_with_followers.html.erb
app/views/follows/_show_without_followings.html.erb
Here is an answer I found so far. I will be using in my view
<%= link_to 'Followers', follows_path(:follows => 'followers') %>
<%= link_to 'Followings', follows_path(:follows => 'followings') %>
and use
def index
if params["follows"] == 'followers'
#render followers
else
#render followings
end
end
end

How to link my courses/show.html.erb to the current_course?

I'm creating an app that allows users to access different courses on video (each course has its own 4 or 5 videos, and each video has its own page).
I created a courses_controller which allows me to index and show the courses stored in the database.
Courses_controller.rb :
class CoursesController < ApplicationController
before_action :set_course, only: [:show]
skip_before_filter :verify_authenticity_token
before_filter :authorize , except: [:index, :show]
def index
#courses = Course.all
end
def show
end
private
# Use callbacks to share common setup or constraints between actions.
def set_course
#course = Course.find(params[:id])
end
def authorize
unless User.find_by_id(session[:user_id]) and User.find_by_id(session[:user_id]).active == true
redirect_to subscriptions_path, :notice => "Vous devez souscrire à un abonnement pour avoir accès a cette page"
end
end
Each course is stored in a seeds.rb file.
courses/index.html.erb lists the courses, courses/show.html.erb shows the presentation of a specific course. These 2 parts are OK.
How to create a link to the current course from the show.html.erb page?
I mean, if I have "course1" and "course2", the link will redirect "( show.html.erb )course1 presentation" to "( ?.html.erb )course1 first video" and "( show.html.erb )course2 presentation" to "( ?.html.erb )course2 first video", etc.
Any idea?
On your index you are doing right.
def index
#courses = Course.all
end
Now in index view you should have link to show page related to each course.
<% #courses.each do |course| %>
<tr>
....
....
<td><%= link_to( 'show', course_path(course) ) %></td>
</tr>
<% end %>
To show all videos related to a course in show page start from first video, You should use kaminari or will paginate. So your show action should look like.
def show
#course = Course.find(params[:id]) #if you want to show any detail of course
#videos = #course.videos.paginate(:page => params[:page], :per_page => 1)
end
Now you will get first video of course in first show request and on each pagination click. you will see next one.
<%= #course.name %>
<%= #videos.first.url %> ##because paginate return array always
## to paginate videos
<%= will_paginate #videos %>

Rendering a rails partial inside of another partial

I have a page listing many products, at taxons#show. Inside of taxons#show I'm rendering the partial _products.html.erb to display the products, images, variants, etc. By default, when you click an individual product on taxons#show rails redirects you to the products#show page where the partial _cart_form.html.erb is rendered which displays add to cart options.
However, I'm trying to render _cart_form.html.erb inside a lightbox in _products.html.erb, which is inside of taxons#show. My problem is that _products.html.erb is using
<% products.each do |product| %>
to display each individual product. And _cart_form.html.erb is using #product to display all of its product info. If I keep it this way I get a NoMethod error for nilClass because #product isn't defined.
Then I tried:
<% #product = product %>
<%= render 'spree/shared/cart_form' %>
But because this is above the code that outputs all of the products on taxons#show it changes every product inside _product.html.erb to the same product, the first one listed.
How can I render _cart_form.html.erb inside of a lightbox for each individual product?
Here's taxons_controller.rb:
def show
#taxon = Taxon.find_by_permalink(params[:id])
return unless #taxon
#taxon_id = params[:id].split('/').first
if #taxon_id
instance_variable_set "#enable_#{#taxon_id}", true #big lettered taxonomy heading
end
#product_type = params[:id].split('/').last
#featured = #taxon_id + '/' + #product_type #featured image
#searcher = Spree::Config.searcher_class.new(params.merge(:taxon => #taxon.id))
#searcher.current_user = try_spree_current_user
#searcher.current_currency = current_currency
#products = #searcher.retrieve_products
#related_products = #taxon.products.offset(rand(Spree::Product.count - 7)).limit(7)
respond_with(#taxon)
And products_controller.rb:
def show
return unless #product
#variants = #product.variants_including_master.active(current_currency).includes([:option_values, :images])
#product_properties = #product.product_properties.includes(:property)
referer = request.env['HTTP_REFERER']
if referer
begin
referer_path = URI.parse(request.env['HTTP_REFERER']).path
# Fix for #2249
rescue URI::InvalidURIError
# Do nothing
else
if referer_path && referer_path.match(/\/t\/(.*)/)
#taxon = Taxon.find_by_permalink($1)
end
end
end
respond_with(#product)
end

Resources