act_as_taggable Gem Ruby on Rails错误:Posts #index中的NoMethodError

时间:2015-10-24 17:12:06

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

我正在尝试使用act_as_taggable Gem将标签添加到我的帖子脚手架,但是当我查看索引或显示页面时出现错误。我跟着这个guide,但我仍然得到错误。

enter image description here

这是我的代码:

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

视图/帖/ _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 %>

视图/帖/ 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 %>

视图/帖/ 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 %>

配置/ routes.rb中

Rails.application.routes.draw do
  resources :posts
  get 'tags/:tag', to: 'pins#index', as: :tag
end

有没有人有解决这个问题的方法?

1 个答案:

答案 0 :(得分:1)

您在一系列帖子上呼叫tags,但它是具有标签的各个帖子。 选择一个帖子并在其上运行tags,或使用Tag.all或类似内容查找系统中的所有标签。