在rails中标记索引:添加第二个模型并将其作为标记
似乎无法找到我的具体问题的答案。
我有一个rails 4应用程序,它显示博客(模型是post)和一个投资组合(模型是pletool)。 我的帖子模型工作正常,因为我的pletool模型除了我无法复制我的索引控制器从帖子pletools。
我使用标签作为标签,并且有两种不同的标签类型 - 标签的“标签”和标签的“标签”。
对于这两个索引视图,我希望能够显示最受欢迎标签的标签云,如果用户点击该标签云,将只过滤到该特定标签的帖子或pletools。
当我尝试将我的pletools_controller.rb中的索引定义从“tags”更改为“pletags”时,我得到'undefined method nil class'错误。
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>
经过多次搜索后(没有我可以在网上找到的真实答案)并且使用各种参数来玩游戏,事实证明我在几个脚本中遇到了一些非常小的问题。
首先为pletools_controller.rb
我需要将@pletool改成复数,例如几行
@pletools = Pletool.tagged_with(params[:pletag]).paginate(:page => params[:page], :per_page => 20)
也在我的routes.rg我已经配置了两个单独的索引路线,我需要做一个小的改变,从'pletag:tag'获得'pletag:pletag'
这里是我最后routes.rb的相关部分
get 'pletags/:pletag', to: 'pletools#index', as: :pletag, layout: 'application'
get 'tags/:tag', to: 'posts#index', as: :tag, layout: 'application'
希望这有助于别人一天/某天!
链接地址: http://www.djcxy.com/p/36015.html上一篇: Tag index in rails: adding a second model with act as taggable