将我的注释添加到索引视图中...... Ruby on Rails

时间:2009-11-17 05:59:50

标签: ruby-on-rails controller blogs comments

好的......我是rails的新手,所以这可能是一个愚蠢的问题,但需要帮助。我刚刚观看并实施了关于建立博客的Ryan Bates screencaset。你可以在http://media.rubyonrails.org/video/rails_blog_2.mov看到它。这是瘦的:

两个表:帖子和评论。一切都很好,包括通过AJAX添加评论。此博客的默认开发为您提供index.html.erb视图,您可以在其中查看所有帖子

  def index
    @posts = Post.all


    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
      format.json { render :json => @posts }
      format.atom
    end
  end

评论只能通过show.html.erb页面查看,并通过该文件中的代码显示:

<%= render :partial => @post %>
<p>
 <%= link_to 'Edit', edit_post_path(@post) %> |
 <%= link_to 'Destroy', @post, :method => :delete, :confirm => "Are You Sure" %> |
 <%= link_to 'See All Posts', posts_path %>
</p>

<h2>Comments</h2>
<div id="comments">
 <%= render :partial => @post.comments %>
</div>

<% remote_form_for [@post, Comment.new] do |f| %>
 <p>
  <%= f.label :body, "New Comment" %><br/>
  <%= f.text_area :body %>
 </p>
 <p><%= f.submit "Add Comment"%></p>
<% end %>

我想要做的是获得index.html.erb视图中存在的注释功能的similair表示(我将使用javascript隐藏)。目前看起来像这样:

<h1>Listing posts</h1>
<%= render :partial => @posts %>

<%= link_to 'New post', new_post_path %>

我最初的想法是将这个完全相同的代码放在index.html.erb文件中的show.html.erb文件中,但这不起作用。我在这里尝试了很多东西,但我对Rails(或编码)的熟悉程度还不够及时。我得到两个主要错误。要么是我传递了nil.comments错误,要么是未定义的对象/方法(不记得了)。

我的问题是我需要包含在post_controller,comments_controller和index.html.erb文件中才能完成此任务。为了完整,我在下面的每个代码中都包含了代码。

POSTS_CONTROLLER

class PostsController < ApplicationController
  before_filter :authenticate, :except => [:index, :show]

  # GET /posts
  # GET /posts.xml
  def index
    @posts = Post.all


    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
      format.json { render :json => @posts }
      format.atom
    end
  end

  # GET /posts/1
  # GET /posts/1.xml
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.xml
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.xml
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        flash[:notice] = 'Post was successfully created.'
        format.html { redirect_to(@post) }
        format.xml  { render :xml => @post, :status => :created, :location => @post }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.xml
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        flash[:notice] = 'Post was successfully updated.'
        format.html { redirect_to(@post) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @post.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.xml
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to(posts_url) }
      format.xml  { head :ok }
    end
  end

  private

  def authenticate
    authenticate_or_request_with_http_basic do |name, password|
      name == "admin" && password == "secret"
    end
  end
end

COMMENTS_CONTROLLER

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create!(params[:comment])
    respond_to do |format|
      format.html { redirect_to @post}
      format.js
    end
  end
end

INDEX.HTML.ERB

<h1>Listing posts</h1>
<%= render :partial => @posts %>

<%= link_to 'New post', new_post_path %>

1 个答案:

答案 0 :(得分:2)

这是一个简单的解决方案。第1步,编辑索引操作以包含属于每个帖子的所有评论。

def index
  @posts = Post.all(:include => :comments)

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @posts }
    format.json { render :json => @posts }
    format.atom
  end
end

步骤2,编辑索引视图以显示每个帖子,然后显示其评论:

<h1>Listing posts</h1>

<% @posts.each do |post| %>
  <%= render :partial => post %>
  <%= render :partial => post.comments %>
<% end %>

<%= link_to 'New post', new_post_path %>

编辑:我不是100%确定包含评论创建表单的最佳方式。我首先尝试的是(在index.html.erb中):

尝试将索引视图更改为:

<h1>Listing posts</h1>

<% @posts.each do |post| %>

  <%= render :partial => post %>
  <%= render :partial => post.comments %>

  <% remote_form_for [post, Comment.new] do |f| %>
    <p>
    <%= f.label :body, "New Comment" %><br/>
    <%= f.text_area :body %>
    </p>
    <p><%= f.submit "Add Comment"%></p>
  <% end %>

<% end %>

<%= link_to 'New post', new_post_path %>

这应该在该帖子的评论下呈现给定帖子的“新评论”表单,但我不确定(实际上没有尝试)表单提交AJAX是否将成功更新并刷新索引页面。