Mr. Z.
Mr. Z.

Reputation: 348

How to render form_for for a nested route on the parent index page?

I'm following a tutorial on youtube. I have posts and they have comments. On the posts show page I'm displaying the comments and the comment form as well. However I want to display the comments and it's form on the posts index page, where I display all the posts.

Here is the code:

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?" } %>

This works well, but when I try to display the same thing on the index page I get different errors.

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

Now I'm getting undefined method comments in the _form.htm.erb file. Basically my question is: How can I successfully display the comments and the comment form on the posts index page under each post?

Here is the rest of my code:

  #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

Upvotes: 0

Views: 584

Answers (2)

sourcx
sourcx

Reputation: 974

I think you should pass along the local object(s) you want your partial to render. This will remove the dependency of your partial on the @form variable and then it can be used anywhere.

Something along the lines will do:

in show.html.erb:

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

and in index.html.erb (within the each):

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

and then use the variable post in your partial instead of @post

Edit: Updated variable names, thanks IvanSelivanov :)

Upvotes: 4

IvanSelivanov
IvanSelivanov

Reputation: 750

Frank Evers's answer is correct, but one thing, not @form and form, but @post and post. Like this:

in show.html.erb:

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

and in index.html.erb (within the each):

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

and then use the variable post in your partial instead of @post

Upvotes: 1

Related Questions