Ruby on Rails 4:无法编辑帖子或评论

时间:2015-11-15 22:45:51

标签: ruby-on-rails

以下是我的应用设置方式。

#app/models/category.rb
class Category < ActiveRecord::Base
    belongs_to :user
    has_many :forums
end

#app/models/forum.rb
class Forum < ActiveRecord::Base
    belongs_to :user
    belongs_to :category
    has_many :posts
end


#app/models/post.rb
class post < ActiveRecord::Base
    belongs_to :user
    belongs_to :forum
    has_many :comments
end

#app/models/comment.rb
class Comment < ActiveRecord::Base
    belongs_to :user
    belongs_to :post
end

我的评论记录存在问题。我可以显示它们并创建它们,但不能编辑/删除它们,因为我无法确定要设置的链接。

这是我的config / routes.rb

Rails.application.routes.draw do
  devise_for :users, :path => '', :path_names => {:sign_up => 'register', :sign_in => 'login', :sign_out => 'logout'}

  resources :categories
  resources :forums do
    resources :posts do
      resources :comments
    end
  end
  root 'categories#index'
end

这是我的评论控制器。

class CommentsController < ApplicationController
  def create
    @forum = Forum.find(params[:forum_id])
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment].permit(:comment))
    @comment.user_id = current_user.id if current_user
    @comment.save

    if @comment.save
      redirect_to [@forum, @post]
    else
      render 'new'
    end
  end

  def edit
    @forum = Forum.find(params[:forum_id])
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
  end

  def update
    @forum = Forum.find(params(:forum_id])
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])

    if @comment.update(params[:comment].permit(:comment))
      redirect_to post_path(@post)
    else
      render 'edit'
    end
  end

  def destroy
    @forum = Forum.find(params[:foumd_id])
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy
    redirect_to post_path(@post)
  end
end

_comments.html.haml

.comment.clearfix
    .content
        %p.comment_content= comment.comment
        %p.comment_author= comment.user.email
    .buttons
        = link_to "Edit", edit_forum_post_comment_path[@forum, @post]

我在编辑帖子时遇到问题,我可以创建它们没问题,但无法编辑它们......

帖子控制器

class PostsController < ApplicationController
  before_action :find_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @posts = Post.all
  end

  def show
  end

  def new
    @forum = Forum.find(params[:forum_id])
    @post = current_user.posts.build
  end

  def create
    @forum = Forum.find(params[:forum_id])
    @post = current_user.posts.build(post_params)

    if @post.save
      redirect_to [@forum, @post]
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    @forum = Forum.find(params[:forum_id])
    if @post.update(post_params)
      redirect_to [@forum, @post]
    else
      render 'edit'
    end
  end

  def destroy
    @post.destroy
    redirect_to root_path
  end

  private

  def find_post
    @post = Post.find(params[:id])
  end

  def post_params
    params.require(:post).permit(:title, :content)
  end
end

日志


I, [2015-11-15T16:10:25.073062 #18541]  INFO -- : Started GET "/forums/1/posts/1/" for 24.220.125.144 at 2015-11-15 16:10:25 -0600
I, [2015-11-15T16:10:25.122253 #18541]  INFO -- : Processing by PostsController#show as HTML
I, [2015-11-15T16:10:25.122482 #18541]  INFO -- :   Parameters: {"forum_id"=>"1", "id"=>"1"}
D, [2015-11-15T16:10:25.148865 #18541] DEBUG -- :   ^[[1m^[[36mPost Load (0.5ms)^[[0m  ^[[1mSELECT  `posts`.* FROM `posts` WHERE `posts$
D, [2015-11-15T16:10:25.185328 #18541] DEBUG -- :   ^[[1m^[[35m (0.4ms)^[[0m  SELECT COUNT(*) FROM `comments` WHERE `comments`.`post_id$
D, [2015-11-15T16:10:25.187051 #18541] DEBUG -- :   ^[[1m^[[36mComment Load (0.3ms)^[[0m  ^[[1mSELECT `comments`.* FROM `comments` WHER$
D, [2015-11-15T16:10:25.210686 #18541] DEBUG -- :   ^[[1m^[[35mUser Load (0.4ms)^[[0m  SELECT  `users`.* FROM `users` WHERE `users`.`id$
I, [2015-11-15T16:10:25.232349 #18541]  INFO -- :   Rendered comments/_comment.html.haml (35.4ms)
I, [2015-11-15T16:10:25.232600 #18541]  INFO -- :   Rendered posts/show.html.haml within layouts/application (69.7ms)
I, [2015-11-15T16:10:25.233044 #18541]  INFO -- : Completed 500 Internal Server Error in 110ms (ActiveRecord: 7.1ms)
F, [2015-11-15T16:10:25.235567 #18541] FATAL -- :
ActionView::Template::Error (No route matches {:action=>"edit", :controller=>"comments", :forum_id=>"1", :id=>"1"} missing required key$
    3:          %p.comment_content= comment.comment
    4:          %p.comment_author= comment.user.email
    5:  .buttons
    6:          = link_to "Edit", edit_forum_post_comment_path[@forum, @post]
  app/views/comments/_comment.html.haml:6:in `_app_views_comments__comment_html_haml___1270861655862681835_22931440'
  app/views/posts/show.html.haml:5:in `_app_views_posts_show_html_haml__2051422644921131492_20982460'


I, [2015-11-15T16:10:28.372822 #18541]  INFO -- : Started GET "/forums/1/posts/1/edit" for 24.220.125.144 at 2015-11-15 16:10:28 -0600
I, [2015-11-15T16:10:28.375577 #18541]  INFO -- : Processing by PostsController#edit as HTML
I, [2015-11-15T16:10:28.375656 #18541]  INFO -- :   Parameters: {"forum_id"=>"1", "id"=>"1"}
D, [2015-11-15T16:10:28.377175 #18541] DEBUG -- :   ^[[1m^[[36mPost Load (0.4ms)^[[0m  ^[[1mSELECT  `posts`.* FROM `posts` WHERE `posts$
D, [2015-11-15T16:10:28.379458 #18541] DEBUG -- :   ^[[1m^[[35mUser Load (0.3ms)^[[0m  SELECT  `users`.* FROM `users` WHERE `users`.`id$
I, [2015-11-15T16:10:28.403726 #18541]  INFO -- :   Rendered posts/_form.html.haml (20.6ms)
I, [2015-11-15T16:10:28.403924 #18541]  INFO -- :   Rendered posts/edit.html.haml within layouts/application (22.8ms)
I, [2015-11-15T16:10:28.404187 #18541]  INFO -- : Completed 500 Internal Server Error in 28ms (ActiveRecord: 0.7ms)
F, [2015-11-15T16:10:28.405890 #18541] FATAL -- :
ActionView::Template::Error (undefined method `post_path' for #:0x00000003e04f18>):
    1: = simple_form_for [@forum,@post] do |f|
    2:  = f.input :title
    3:  = f.input :content
    4:  = f.submit
  app/views/posts/_form.html.haml:1:in `_app_views_posts__form_html_haml__3317057176452412006_32596640'
  app/views/posts/edit.html.haml:3:in `_app_views_posts_edit_html_haml__3955498671314368903_32545500'

1 个答案:

答案 0 :(得分:0)

错误如下:

  

ActionView :: Template ::错误(没有路由匹配{:action =&gt;&#34;编辑&#34;,:controller =&gt;&#34;评论&#34;,:forum_id =&gt;&# 34; 1&#34;,:id =&gt;&#34; 1&#34;}缺少必需的密钥$

     

ActionView :: Template :: Error(未定义的方法`post_path&#39; for#:0x00000003e04f18&gt;):

一般在提问/调试时,您需要从错误/问题开始。

你已经提出了如此多的代码,大多数人都会嗤之以鼻。

-

该错误表明您的路线

存在问题
#config/routes.rb
Rails.application.routes.draw do

  resources :categories
  resources :forums do
    resources :posts do
      resources :comments
    end
  end
  root 'categories#index'
end

问题似乎是您嵌套forms / posts / comments的方式:

第一个错误是因为您未通过post_id

<%= link_to "Edit Comment", edit_forum_post_comment_path(@forum, @post, @comment) %>

第二个错误是因为(我认为)您在没有填充@forum变量的情况下调用了您的路径。您应确保填充@forum,如下所示:

@forum = Forum.find params[:forum_id]
@post = Post.new

或者,您可以在post对象上构建@forum对象,如下所示:

@forum = Forum.find params[:forum_id]
@post = @forum.posts.build

这应该允许您拨打<%= form_for @post do |f| %>

作为辅助设备,您需要查找limits to nesting,因为它直接处理您的问题:

  

资源不应该嵌套超过1级。

虽然我不得不花一些时间考虑解决问题的最佳方法,但我会做以下事情:

#config/routes.rb
resources :forums do 
   resources :posts, :comments
end

这会将comments直接设置在forums资源下方,这样您每次想要对forumpost进行任何操作时都可以跳过此关联...

<%= link_to "Edit Comment", edit_forums_comments_path(@forum, @comment) %>