如何在父索引页面上为嵌套路由呈现form_for?

时间:2015-04-07 19:48:04

标签: ruby-on-rails ruby ruby-on-rails-4

我正在关注youtube上的教程。我有帖子,他们有评论。在帖子显示页面上我也显示了评论和评论表格。 但是我想在帖子索引页面上显示评论及其表格,我会在其中显示所有帖子。

以下是代码:

show.html.erb

<p><%= notice %></p>

<p>
  <strong>Content:</strong>
  <%= @post.content %>
</p>

<div id="comments_wrapper">
    <%= render @post.comments %>
    <div id="form">
        <%= render "comments/form" %>
    </div>
</div>

<%= link_to 'Edit', edit_post_path(@post) %>
<%= link_to 'Back', posts_path %>

_form.html.erb

<%= form_for([@post, @post.comments.build]) do |f| %>
    <%= f.text_field :content, placeholder: "New Comment" %>
    <%= f.submit %>
<% end %>

_comment.html.erb

<p><%= comment.content %></p>
<%= link_to "delete", post_comment_path(@post, comment.id), method: :delete, data: { confirm: "Are you sure?" } %>

这很好用,但是当我尝试在索引页面上显示相同的内容时,我会遇到不同的错误。

index.html.erb

<h2>Posts</h2>
    <% @posts.each do |post| %>
        <%= post.content %>
        <%= link_to 'Show', post %>
        <%= link_to 'Edit', edit_post_path(post) %>
        <%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
        <br>

        <div id="comments_wrapper">
            #first i got an error here, I solved it by removing the @ before the post
            <%= render post.comments %>
            <div id="form">
                <%= render "comments/form" %>
            </div>
        </div>
    <% end %>
<br>
<%= link_to 'New Post', new_post_path %>

现在我在 _form.htm.erb 文件中收到未定义的方法注释。 基本上我的问题是:如何在每个帖子下的帖子索引页面上成功显示评论和评论表单?

以下是我的其余代码:

  #routes.rb
  resources :posts  do
    resources :comments
  end
  #post.rb
  has_many :comments
  #comment.rb
  belongs_to :post


  #comments_controller.rb
  before_action :set_post

  def create
    @comment = @post.comments.create(comment_params)
    redirect_to @post
  end

  def destroy
    @comment = @post.comments.find(params[:id])
    if @comment.destroy
      flash[:success] = "Comment was deleted."
    else
      flash[:error] = "Comment could not be deleted."
    end
    redirect_to @post
  end

  private

  def set_post
    @post = Post.find(params[:post_id])
  end

  def comment_params
    params[:comment].permit(:content)
  end

#posts_controller.rb
before_action :set_post, only: [:show, :edit, :update, :destroy]

  def index
    @posts = Post.all
  end

  def show
  end

  def new
    @post = Post.new
  end

  def edit
  end

  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

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

2 个答案:

答案 0 :(得分:4)

我认为你应该传递你想要部分呈现的本地对象。这将删除部分对@form变量的依赖关系,然后可以在任何地方使用它。

有些事情会做:

show.html.erb中的

  <%= render "comments/form", post: @post %>

并在index.html.erb中(在每个内):

  <%= render "comments/form", post: post %>

然后在部分代替post

中使用变量@post

编辑:更新了变量名称,谢谢IvanSelivanov:)

答案 1 :(得分:1)

Frank Evers的回答是正确的,但有一件事,不是@formform,而是@postpost。 像这样:

  show.html.erb中的

     

<%= render "comments/form", post: @post %>

     

并在index.html.erb中(在每个内):

     

<%= render "comments/form", post: post %>

     

然后在部分代替post

中使用变量@post