-1

Я пытаюсь добавить теги в свои сообщения Scaffold с помощью act_as_taggable Gem, но я получаю сообщение об ошибке при просмотре индекса или страницы показа. Я следил за этим guide, но я все еще получаю ошибку.act_as_taggable Gem Ruby on Rails Ошибка: NoMethodError в сообщениях # index

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 %> 

config/routes.rb

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

У кого-нибудь есть решение этой проблемы?

+0

Пожалуйста, разместите здесь ошибку здесь вместо прикрепленного экрана. Его очень трудно читать. – Pavan

ответ

1

Вы звоните tags в коллекцию сообщений, но это отдельные сообщения, содержащие теги. Либо выберите одно сообщение и запустите tags, либо используйте Tag.all или что-то подобное, чтобы найти все теги в системе.