【问题标题】:Tag index in rails: adding a second model with act as taggableRails 中的标记索引:添加第二个具有可标记功能的模型
【发布时间】:2015-10-22 09:07:21
【问题描述】:

似乎无法为我的具体问题找到答案。

我有一个 rails 4 应用程序,它同时显示博客(模型是帖子)和投资组合(模型是 pletool)。我的帖子模型和我的 pletool 模型一样工作正常,只是我无法将我的索引控制器从帖子复制到 pletools。

我使用 act as taggable 来标记并有两种单独的标记类型 - 用于帖子的“tags”和用于 pletools 的“pletags”。

对于这两个索引视图,我希望能够显示最流行标签的标签云,如果用户随后点击它将过滤到该特定标签的帖子或 pletools。

当我尝试将 pletools_controller.rb 中的索引定义从“tags”更改为“pletags”时,出现“未定义方法 nil 类”错误。

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>

【问题讨论】:

    标签: ruby-on-rails tagging


    【解决方案1】:

    经过大量搜索(我无法在网上找到真正的答案)并尝试了各种参数后,我发现我的几个脚本中有一些非常小的问题。

    首先用于 pletools_controller.rb

    我需要在几行中将@pletool 更改为复数,例如

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

    同样在我的 routes.rg 中,我配置了两个单独的索引路由,我需要再次从获取 'pletag:tag' 到获取 'pletag:pletag' 进行细微更改

    这是我最终路线的相关部分。rb

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

    希望有一天/有一天这可以帮助其他人!

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 2015-07-02
      • 1970-01-01
      • 2022-10-20
      • 2020-07-22
      • 2019-05-07
      • 2017-10-02
      • 2011-11-11
      • 1970-01-01
      相关资源
      最近更新 更多