Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails act_as_taggable Gem Ruby on Rails错误:帖子中的nomethoder错误#索引_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 4_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails act_as_taggable Gem Ruby on Rails错误:帖子中的nomethoder错误#索引

Ruby on rails act_as_taggable Gem Ruby on Rails错误:帖子中的nomethoder错误#索引,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Ruby On Rails 3.2,我试图使用act_as_taggable Gem将标记添加到我的帖子脚手架中,但在查看索引或显示页面时出错。我已经遵循了这一点,但我仍然得到了错误 这是我的密码: posts\u controller.rb class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] # GET /posts # GET /post

我试图使用act_as_taggable Gem将标记添加到我的帖子脚手架中,但在查看索引或显示页面时出错。我已经遵循了这一点,但我仍然得到了错误

这是我的密码:

posts\u 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
class Post < ActiveRecord::Base
  acts_as_taggable
  mount_uploaders :image, ImageUploader

end
<%= 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 %>
<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 %>
<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 %>
Rails.application.routes.draw do
  resources :posts
  get 'tags/:tag', to: 'pins#index', as: :tag
end

有人找到了这个问题的解决方案吗?

您在一组帖子上调用了
标签,但只有个别帖子才有标签。

选择单个post并在其上运行
tags
,或使用
Tag.all
或类似方法查找系统中的所有标签。

请在此处发布错误,而不是附加屏幕截图。它很难读。