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

发布于 2024-08-12 01:27:21 字数 4558 浏览 1 评论 0原文

好吧...我是 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 %>

Ok...I am new to rails so this may be a stupid question but need help. I just watched and implemented Ryan Bates screencaset about setting up a blog. You can see it here http://media.rubyonrails.org/video/rails_blog_2.mov. Here is the skinny:

Two tables: Posts and Comments. Everything works great including the addition of comments via AJAX. The default development of this blog gives you the index.html.erb view where you can view all the posts

  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

The comments are only viewed via the show.html.erb page and is displayed via this code in that file:

<%= 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 %>

What I am trying to do is to get similair representation of the comments functionality to exist in the index.html.erb view (which I will hide with javascript). Which currently just looks like this:

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

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

My initial thought was just to put this exact same code that is in the show.html.erb file in the index.html.erb file but that doesn't work. I have tried a bunch of things here but I am not familiar enough with Rails (or coding for that matter) yet to do this in a timely manner. I get two main errors. Either that I passed a nil.comments error or an undefined object/method (can't remember).

My question is what do I need to included in the post_controller, the comments_controller and the index.html.erb file to accomplish this. To be complete I have included the code in each below.

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 %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2024-08-19 01:27:21

这是一个简单的解决方案。第 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是否会成功更新和刷新索引页面。

Here's one simple solution. Step 1, edit your index action to include all the comments belonging to each post.

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

Step 2, edit your index view to display each post, followed by its comments:

<h1>Listing posts</h1>

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

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

Edit: I'm not 100% sure of the best way to include the comment creation form also. What I'd try first is this (in index.html.erb):

Try changing your index view to:

<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 %>

This should render the "New comment" form for a given post under the comments for that post, but I'm not sure (without actually trying it out) whether the form submission AJAX will successfully update and refresh the index page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文