Кажется, не могу найти ответ на мою конкретную проблему.
У меня есть приложение rails 4, которое отображает как блог (модель - это сообщение), так и портфолио (модель - это pletool). Моя модель постов работает нормально, как и моя модель плетула, за исключением того, что я не могу реплицировать свой контроллер индекса из постов в плетуль.
Я использую act as taggable для добавления тегов и имею два отдельных типа тегов - «теги» для сообщения и «теги» для инструментов.
Для обоих представлений индекса я хочу иметь возможность отображать облако тегов самых популярных тегов, которое, если пользователь затем нажимает, будет фильтровать только сообщения или полные инструменты этого конкретного тега.
Я получаю сообщение об ошибке undefined method nil class, когда пытаюсь изменить определение индекса в моем pletools_controller.rb с «тегов» на «pletags».
pletools_controller.rb
class PletoolsController < ApplicationController
layout 'application'
before_action :find_pletool, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :tag_cloud
def index
if params[:pletag].present?
@pletool = Pletool.tagged_with(params[:pletag]).paginate(:page => params[:page], :per_page => 20)
else
@pletools = Pletool.all.order('created_at DESC').paginate(page: params[:page], per_page: 20)
end
end
def tag_cloud
@pletags = Pletool.tag_counts_on(:pletags, :limit => 10, :order => "count desc")
end
def new
@pletool = Pletool.new
end
def show
end
def edit
end
def create
@pletool = Pletool.new(pletool_params)
if @pletool.save
redirect_to @pletool, notice: "Tool succesfully saved!"
else
render 'new', notice: "Try Again. I was unable to save your PLE Tool."
end
end
def update
if @pletool.update(params[:pletool].permit(:title, :description, :link, :image, :pletag_list))
redirect_to @pletool, notice: "PLE Tool succesfully edited!"
else
render 'edit'
end
end
def destroy
@pletool.destroy
respond_to do |format|
format.html { redirect_to pletools_url, notice: 'Ple Tool was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def pletool_params
params.require(:pletool).permit(:title, :link, :description,:image, :image_url, :slug, :pletag, :pletag_list)
end
def find_pletool
@pletool = Pletool.friendly.find(params[:id])
end
end
index.html.erb
<% @pletools.each do |pletool| %>
<li class="post msnry-blog-entry #">
<% if pletool.image.exists? %>
<div class="click-slide click-top">
<%= link_to image_tag pletool.image.url(:large) %>
<div class = "text-center">
<%= link_to '<span class = "ti ti-eye"></span>'.html_safe, pletool %>
</div>
</div>
<% end %>
<div class="msnry-blog-entry-text">
<h4 class"h5"><a href="#"><%= link_to pletool.title, pletool %></a></h4>
<p><%= markdown truncate(pletool.description, :length => 150) %></p>
<ul class="msnry-blog-entry-meta list-inline">
<li>
<span class="ti ti-pencil-alt">
</span>
by Anthony Llewellyn
</li>
<li>
<span class="ti ti-calendar">
</span>
<%= pletool.created_at.strftime('%A, %B %d') %>
</li>
<li class="entry-tags hidden-xs">
<% pletool.pletags.any? %>
<% pletool.pletags.each do |tag| %>
<li><a href="#">
<%= link_to tag.name, pletag_path(tag.name) %>
</a></li>
<% end %>
</li>
</ul>
</div>
</li>
<% end %>
</li>
</ul>