act_as_taggable Gem Ruby on Rails Error: NoMethodError in Posts#index
act_as_taggable Gem Ruby on Rails Error: NoMethodError in Posts#index
我正在尝试使用 act_as_taggable Gem 将标签添加到我的帖子支架中,但是当我查看索引或显示页面时出现错误。我已经按照这个 guide 但我仍然得到错误。
这是我的代码:
posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
@posts = Post.tagged_with(params[:tag])
@posts = Post.all.order("created_at DESC")
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:name, {image:[]}, :tag_list)
end
end
post.rb
class Post < ActiveRecord::Base
acts_as_taggable
mount_uploaders :image, ImageUploader
end
views/posts/_form.html.erb
<%= form_for(@post , html: { multipart: true }) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :image , multiple: true %>
</div>
<div class="form-group">
<%= f.label :tag_list, "Tags (separated by commas)" %>
<%= f.text_field :tag_list, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
views/posts/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Posts</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<%= @posts.tags.each do |tag| %>
<span class="label label-teal">
<%= link_to tag.name, tag_path(tag.name) %>
</span>
<% end %>
<br>
<%= link_to 'New Post', new_post_path %>
views/posts/show.html.erb
<p id="notice"><%= notice %></p>
<%= image_tag @post.image%>
<p>
<strong>Name:</strong>
<%= @post.name %>
</p>
<% @posts.tags.each do |tag| %>
<span class="label label-teal">
<%= link_to tag.name, tag_path(tag.name) %>
</span>
<% end %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
config/routes.rb
Rails.application.routes.draw do
resources :posts
get 'tags/:tag', to: 'pins#index', as: :tag
end
有人解决了这个问题吗?
您正在对一组帖子调用 tags
,但它是具有标签的单个 post。
select 单个 post 和 运行 tags
,或者使用 Tag.all
或类似的东西来查找系统中的所有标签。
我正在尝试使用 act_as_taggable Gem 将标签添加到我的帖子支架中,但是当我查看索引或显示页面时出现错误。我已经按照这个 guide 但我仍然得到错误。
这是我的代码:
posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
@posts = Post.tagged_with(params[:tag])
@posts = Post.all.order("created_at DESC")
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:name, {image:[]}, :tag_list)
end
end
post.rb
class Post < ActiveRecord::Base
acts_as_taggable
mount_uploaders :image, ImageUploader
end
views/posts/_form.html.erb
<%= form_for(@post , html: { multipart: true }) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :image , multiple: true %>
</div>
<div class="form-group">
<%= f.label :tag_list, "Tags (separated by commas)" %>
<%= f.text_field :tag_list, class: "form-control" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
views/posts/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Posts</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<%= @posts.tags.each do |tag| %>
<span class="label label-teal">
<%= link_to tag.name, tag_path(tag.name) %>
</span>
<% end %>
<br>
<%= link_to 'New Post', new_post_path %>
views/posts/show.html.erb
<p id="notice"><%= notice %></p>
<%= image_tag @post.image%>
<p>
<strong>Name:</strong>
<%= @post.name %>
</p>
<% @posts.tags.each do |tag| %>
<span class="label label-teal">
<%= link_to tag.name, tag_path(tag.name) %>
</span>
<% end %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
config/routes.rb
Rails.application.routes.draw do
resources :posts
get 'tags/:tag', to: 'pins#index', as: :tag
end
有人解决了这个问题吗?
您正在对一组帖子调用 tags
,但它是具有标签的单个 post。
select 单个 post 和 运行 tags
,或者使用 Tag.all
或类似的东西来查找系统中的所有标签。