Ruby on rails 在tags/show.html.erb中显示post_tag_链接
嗨,我正在制作公告牌系统,在轨道上使用acts_as_taggable_ 我有个问题,如何在tags/show.html.erb中显示post_tag_链接 我的代码在下面 标记控制器.rbRuby on rails 在tags/show.html.erb中显示post_tag_链接,ruby-on-rails,ruby,Ruby On Rails,Ruby,嗨,我正在制作公告牌系统,在轨道上使用acts_as_taggable_ 我有个问题,如何在tags/show.html.erb中显示post_tag_链接 我的代码在下面 标记控制器.rb def show @tag=ActsAsTaggableOn::Tag.find_by(name: params[:name]) @posts = Post.tagged_with(@tag.name) end <h1><%= @tag.name %></h1>&l
def show
@tag=ActsAsTaggableOn::Tag.find_by(name: params[:name])
@posts = Post.tagged_with(@tag.name)
end
<h1><%= @tag.name %></h1><%= link_to "タグ一覧",tags_path %>
<div><%= render @posts %></div>
acts_as_taggable_on :tags
acts_as_taggable
after_validation :save_tags
def save_tags
array = self.check_taggable_word(self.title)
self.tag_list.add(array, parse: true)
end
def tag_lists
tag_lists = self.tag_list
end
def check_taggable_word(text)
ary = Array.new
nm = Natto::MeCab.new
nm.parse(text) do |n|
ary<<n.surface
end
tags = ActsAsTaggableOn::Tag.pluck(:name)
return ary & tags
end
Rails.application.routes.draw do
root 'static_pages#home'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
resources :categories do
resources :posts
end
resources :posts do
resources :comments
resources :tags
end
get 'tags/:name' => 'tags#show'
resources :tags, only: [:index, :show]
end
def create
category_id = params[:post][:category_id]
if category_id.present? && Category.find_by(id: category_id)
@category = Category.find_by(id: category_id)
else
@post = Post.new(
:title => params[:post][:title],
:name => params[:post][:name]
)
@post.valid?
render :action => "new" and return
end
@post = Post.post_create(post_params)
@post.ip = request.remote_ip
if @post.name.present?
else
@post.name = “no-name”
end
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: ‘done’ }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
标记show.html.erb
def show
@tag=ActsAsTaggableOn::Tag.find_by(name: params[:name])
@posts = Post.tagged_with(@tag.name)
end
<h1><%= @tag.name %></h1><%= link_to "タグ一覧",tags_path %>
<div><%= render @posts %></div>
acts_as_taggable_on :tags
acts_as_taggable
after_validation :save_tags
def save_tags
array = self.check_taggable_word(self.title)
self.tag_list.add(array, parse: true)
end
def tag_lists
tag_lists = self.tag_list
end
def check_taggable_word(text)
ary = Array.new
nm = Natto::MeCab.new
nm.parse(text) do |n|
ary<<n.surface
end
tags = ActsAsTaggableOn::Tag.pluck(:name)
return ary & tags
end
Rails.application.routes.draw do
root 'static_pages#home'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
resources :categories do
resources :posts
end
resources :posts do
resources :comments
resources :tags
end
get 'tags/:name' => 'tags#show'
resources :tags, only: [:index, :show]
end
def create
category_id = params[:post][:category_id]
if category_id.present? && Category.find_by(id: category_id)
@category = Category.find_by(id: category_id)
else
@post = Post.new(
:title => params[:post][:title],
:name => params[:post][:name]
)
@post.valid?
render :action => "new" and return
end
@post = Post.post_create(post_params)
@post.ip = request.remote_ip
if @post.name.present?
else
@post.name = “no-name”
end
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: ‘done’ }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
附加信息
posts\u controller.rb
def show
@tag=ActsAsTaggableOn::Tag.find_by(name: params[:name])
@posts = Post.tagged_with(@tag.name)
end
<h1><%= @tag.name %></h1><%= link_to "タグ一覧",tags_path %>
<div><%= render @posts %></div>
acts_as_taggable_on :tags
acts_as_taggable
after_validation :save_tags
def save_tags
array = self.check_taggable_word(self.title)
self.tag_list.add(array, parse: true)
end
def tag_lists
tag_lists = self.tag_list
end
def check_taggable_word(text)
ary = Array.new
nm = Natto::MeCab.new
nm.parse(text) do |n|
ary<<n.surface
end
tags = ActsAsTaggableOn::Tag.pluck(:name)
return ary & tags
end
Rails.application.routes.draw do
root 'static_pages#home'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
resources :categories do
resources :posts
end
resources :posts do
resources :comments
resources :tags
end
get 'tags/:name' => 'tags#show'
resources :tags, only: [:index, :show]
end
def create
category_id = params[:post][:category_id]
if category_id.present? && Category.find_by(id: category_id)
@category = Category.find_by(id: category_id)
else
@post = Post.new(
:title => params[:post][:title],
:name => params[:post][:name]
)
@post.valid?
render :action => "new" and return
end
@post = Post.post_create(post_params)
@post.ip = request.remote_ip
if @post.name.present?
else
@post.name = “no-name”
end
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: ‘done’ }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
结束实施可标记的行为
包括在post.rb
模型或user.rb
模型中
class Post
acts_as_taggable_on :tags
end
在posts\u controller
或user\u controller
中包含以下强参数
class PostsController < ApplicationController
def user_params
params.require(:posts).permit(:tag_list) ## Rails 4 strong params usage
end
end
将标签添加到新的@post
@post.tag_list.add("history")
@post.tag_list.add("england")
@post.save
检查标签
@post.tag_list
@post.tags
这应该是预期的输出,如果在标记创建过程中出现问题,您需要停止并修复它。如果此标记仅用于此@post中,则标记计数应为1
=> [#<ActsAsTaggableOn::Tag id: 1, name: "history", taggings_count: 1>,
#<ActsAsTaggableOn::Tag id: 2, name: "england", taggings_count: 1>]
我知道您必须检查@tags是否被填充,所以在rails控制台中执行以下操作(我测试了它,它返回了以下内容)。如果它是空的,它就不能工作。
@tags=Post.tag\u计数(:tags)
这是模块
module PostsHelper
include ActsAsTaggableOn::TagsHelper
end
我做了你的代码,然后这个错误显示谢谢你Fabrizio!我是日本学生,两个月前刚开始学习编码:)我想更努力地写代码:)谢谢!!!我刚刚在上面添加了我的帖子_controller.rb!非常感谢法布里齐奥!!我会先试试:)是的,我也不知道。。。是否有必要在Post和Tag之间建立父子关系?hi Fabrizio。我试过。。。然而,我的标签不算“taggings_count”,我不知道为什么。这是我在控制台中检查的内容@tag=>#你好,我刚刚添加了def。但仍会显示此错误。在post.rb的self.tag_list.add的save_tags方法之后,我还没有保存标签。你怎么认为???若我在self.tag\u list.add之后添加self.method,那个么save方法将实现为循环…我尝试了上面的代码,然后显示了这个错误…哦,对不起。。。我忘了。。。然后显示了这个错误