将我的评论添加到索引视图中……Ruby on Rails

问题描述 投票:1回答:2

好...我是Rails的新手,所以这可能是一个愚蠢的问题,但需要帮助。我只是观看并实现了有关设置博客的Ryan Bates屏幕保护程序。您可以在这里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视图中存在注释功能的类似表达(我将使用javascript隐藏该视图)。当前看起来像这样:

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

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

我最初的想法只是将show.html.erb文件中完全相同的代码放入index.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 %>
ruby-on-rails controller blogs comments
2个回答
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是否将成功更新和刷新索引页面。 >


-2
投票

这里是负担得起的虚拟主机的Bluehost coupon code

© www.soinside.com 2019 - 2024. All rights reserved.