Tag index in rails: adding a second model with act as taggable

Can't seem to find an answer for my specific problem.

I have a rails 4 application which displays both a blog (model is post) and a portfolio (model is pletool). My post model is working fine, as is my pletool model except that I am unable to replicate my index controller from posts to pletools.

I'm using act as taggable to tag and have two separate tag types - "tags" for the post and "pletags" for the pletools.

For both index views I want to be able to display a tag cloud of the most popular tags, which if the user then clicks on will filter to only posts or pletools of that particular tag.

I get an 'undefined method nil class' error when I try to alter the index definition in my pletools_controller.rb from "tags" to "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>

ok after much searching (with no real answers that I could find online) and playing around with various parameters, it turns out i had some very minor issues in a couple of my scripts.

firstly for pletools_controller.rb

i needed to change @pletool to plural in a couple of lines eg

@pletools = Pletool.tagged_with(params[:pletag]).paginate(:page => params[:page], :per_page => 20)

also in my routes.rg i have configured two separate index routes and again i needed to make a minor change from get 'pletag:tag' to get 'pletag:pletag'

here is the relevant section of my final routes.rb

  get 'pletags/:pletag', to: 'pletools#index', as: :pletag, layout: 'application'
  get 'tags/:tag', to: 'posts#index', as: :tag, layout: 'application'

Hope that this helps someone else out one day / some day!

链接地址: http://www.djcxy.com/p/36016.html

上一篇: 红宝石在轨道上

下一篇: 在rails中标记索引:添加第二个模型并将其作为标记