Ruby on rails 如何使用RubyonRails从头开始创建博客-从Rails2.x到3.x-无可视评论

Ruby on rails 如何使用RubyonRails从头开始创建博客-从Rails2.x到3.x-无可视评论,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-2,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 2,我正在研究如何使用sixrevisions.com上的RubyonRails教程从头开始创建博客 当我运行服务器并创建新帖子时,我没有可查看的选项来添加评论。根据教程,我应该能够添加结束编辑评论创建的职位 我的评论\u controller.rb: class CommentsController < ApplicationController # GET /comments # GET /comments.json def index @comments

我正在研究如何使用sixrevisions.com上的RubyonRails教程从头开始创建博客

当我运行服务器并创建新帖子时,我没有可查看的选项来添加评论。根据教程,我应该能够添加结束编辑评论创建的职位

我的评论\u controller.rb:

  class CommentsController < ApplicationController
  # GET /comments
  # GET /comments.json
    def index
      @comments = Comment.all

      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @comments }
      end
    end

    # GET /comments/1
    # GET /comments/1.json
     def show
      @comment = Comment.find(params[:id])

       respond_to do |format|
         format.html # show.html.erb
         format.json { render json: @comment }
       end
    end

     # GET /comments/new
     # GET /comments/new.json
    def new
       @comment = Comment.new

       respond_to do |format|
       format.html # new.html.erb
       format.json { render json: @comment }
      end
    end

    # GET /comments/1/edit
    def edit
       @comment = Comment.find(params[:id])
    end

    # POST /comments
    # POST /comments.json
    def create

        @post = Post.find(params[:post_id])
        @comment = @post.comments.create!(params[:comment])
        redirect_to @post
    end


    # PUT /comments/1
    # PUT /comments/1.json
     def update
       @comment = Comment.find(params[:id])

       respond_to do |format|
         if @comment.update_attributes(params[:comment])
         format.html { redirect_to @comment, notice: 'Comment was successfully      
         updated.' }
          format.json { head :no_content }
         else
          format.html { render action: "edit" }
         format.json { render json: @comment.errors, status: :unprocessable_entity }
         end
       end
     end

     # DELETE /comments/1
     # DELETE /comments/1.json
    def destroy
      @comment = Comment.find(params[:id])
      @comment.destroy

      respond_to do |format|
        format.html { redirect_to comments_url }
        format.json { head :no_content }
      end
    end
  end
谢谢你的帮助和关心

您的代码看起来不错

转到
http://localhost:3000/posts/new
并创建帖子。如果这是你的第一篇文章,那么这篇文章将有一个特定的id,大概是1

然后,转到
http://localhost:3000/posts/1/comments
-在那里,您将看到所有评论(可能没有),以及创建新评论的链接。如果没有链接,请转到-您猜对了-
http://localhost:3000/posts/1/comments/new
。这正是你的路线告诉你的

看起来你在用脚手架。我不知道这个教程,但是看看吧

您的代码看起来不错

转到
http://localhost:3000/posts/new
并创建帖子。如果这是你的第一篇文章,那么这篇文章将有一个特定的id,大概是1

然后,转到
http://localhost:3000/posts/1/comments
-在那里,您将看到所有评论(可能没有),以及创建新评论的链接。如果没有链接,请转到-您猜对了-
http://localhost:3000/posts/1/comments/new
。这正是你的路线告诉你的

看起来你在用脚手架。我不知道这个教程,但是看看吧


使用创建新链接

<%= link_to('new comment',new_post_path) %>


在视图模板中

使用

<%= link_to('new comment',new_post_path) %>


在您的视图模板中

在您的终端中运行
rake ROTES
并告诉我们输出是什么,请将rake ROTES资源添加到主柱。在您的终端中运行
rake ROTES
并告诉我们输出是什么,请将rake ROTES资源添加到主柱。Thans!。那些链接非常有用!谢谢!。那些链接非常有用!嗨,我有完全相同的问题!我只是不知道在你的答案上你提到的链接放在哪里@MichaelDurrantHi,我有完全相同的问题!我只是不知道在你的答案@Michaeldurrent上你提到的链接放在哪里
<%= link_to('new comment',new_post_path) %>