Rails 5:找不到没有ID的评论

时间:2017-10-02 15:22:28

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

当我点击edit或'删除'时,我收到此错误链接:Couldn't find Comment without an ID

使用以下设置:

class CommentsController < ApplicationController
before_action :find_item
before_action :find_comment, only: [:destroy, :edit, :update, :comment_owner]
before_action :comment_owner, only: [:destroy, :edit, :update]

  def create
    @comment = @item.comments.create(params[:comment].permit(:content))
    @comment.user_id = current_user.id
    @comment.save

    if @comment.save
      redirect_to item_path(@item)
    else
      render 'comment'
    end
  end

  def destroy
    @comment.destroy
    redirect_to item_path(@item)
  end

  def edit
  end

  def update
    if @comment.update(params[:comment].permit(:content))
      redirect_to item_path (@item)
    else
      render 'edit'
    end
  end

  private

  def comment_owner
    unless current_user.id == @comment.user_id
      flash[:notice] = "You shall not pass!"
      redirect_to @post
    end
  end

  def find_item
    @item = Item.find(params[:item_id])
  end

  def find_comment
    @comment = @item.comments.find(params[:id])
  end
end

应用程序/视图/评论/ comment.html.erb

This Item has<%= pluralize(@comments.count, "Connect") %>


<% @comments.each do |comment| %>
    <p><strong><%= comment.user.username %></strong></p>
    <%= comment.content %>

    <% if current_user == comment.user %>
      <%= link_to 'Edit', edit_item_comments_path(comment.item, comment) %>
      <%= link_to 'Delete Comment', item_comments_path(comment.item, comment), method: :delete, data: { confirm: 'Are you sure?' } %>
    <% end %>
<% end %>


<%= form_for([@item, @item.comments.build]) do |f| %>

  <div class="field">
  <%= f.label :content %>
    <%= f.text_area :content, id: :content %>
    </div>

    <div class="actions">
      <%= f.submit %>
    </div>
<% end %>

routes.rb如果我rake routes | grep comment

 item_comments GET    /items/:item_id/comments(.:format)          comments#index
               POST   /items/:item_id/comments(.:format)          comments#create
 new_item_comment GET    /items/:item_id/comments/new(.:format)      comments#new
 edit_item_comment GET    /items/:item_id/comments/:id/edit(.:format) comments#edit
 item_comment GET    /items/:item_id/comments/:id(.:format)      comments#show
             PATCH  /items/:item_id/comments/:id(.:format)      comments#update
              PUT    /items/:item_id/comments/:id(.:format)      comments#update
            DELETE /items/:item_id/comments/:id(.:format)      comments#destroy

schema.rb

create_table "comments", force: :cascade do |t|
    t.text "content"
    t.bigint "item_id"
    t.bigint "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["item_id"], name: "index_comments_on_item_id"
    t.index ["shopper_id"], name: "index_comments_on_shopper_id"
  end

class Comment < ApplicationRecord
  belongs_to :item
  belongs_to :user
end

class Item < ApplicationRecord
  has_many :comments
end

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  has_many :comments
end

2 个答案:

答案 0 :(得分:1)

每个ActiveRecord的表都应该有主键,但“comments”不能。

选项1(Rails方式)

使用主键(在注释表中添加id字段)。

选项2

使用复合主键(请参阅https://github.com/composite-primary-keys/composite_primary_keys

答案 1 :(得分:1)

我认为问题在于这一行

<%= form_for([@item, @item.comments.build]) do |f| %>

@item.comments.build添加一个新的(不是数据库持久化)注释,然后将其保存到@comments并循环渲染。所以

<%= link_to 'Edit', edit_item_comments_path(comment.item, comment) %>

在页面上显示为

<a href="/items/1/comments//edit">Edit</a>

我建议你尝试改变

<%= form_for([@item, @item.comments.build]) do |f| %>

<%= form_for([@item, Comment.new]) do |f| %>

也可以请确认没有评论ID - 链接编辑/删除页面上的评论?