0

I'm trying to make "posts" nested under "groups" so that when I have a group page I can have posts associated with that particular group.

I'm having a problem at this point just trying to get the "new" template to render for posts.

here my routes

  # nested routes for groups
  resources :groups do
    resources :posts
  end

new action in the posts_controller

before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :set_group, only: [:index, :show, :new, :edit, :create, :update]


# renders the posts/new.html.erb template
def new
  @post = @group.posts.new
end

private

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

 # Use callbacks to share common setup or constraints between actions.
 def set_post
   @post = Post.find(params[:id])
 end

 def set_group
   @group = Group.find(params[:group_id])
 end

Post model (Group model has: has_many :posts, dependent: :destroy)

class Post < ApplicationRecord
  belongs_to :group

  validates :title, :content, presence: true

end

the posts/new template

<%= render partial: "static_pages/navigation" %>

<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><%= link_to 'Posts', group_posts_path, class: "" %></li>
    <li class="breadcrumb-item active" aria-current="page">New Post</li>
  </ol>
</nav>  

<div class="posts_form">
  <div class="text-center title-text">
    <h2>Create a new post</h2>
    <h5>Put some sub text here</h5>
  </div>
  <br>

    <%= render 'form', post: @post %>

</div><!--./posts_form-->

the posts/_form

<div class="">
  <div class="container-fluid">
    <div class="col-md-8 offset-md-2 col-sm-12 test">  

      <%= form_with(model: post, local: true) do |form| %>
        <% if post.errors.any? %>
          <div id="error_explanation">
            <h2 class="text-danger"><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>

            <ul class="text-danger">
            <% post.errors.full_messages.each do |message| %>
              <li><%= message %></li>
            <% end %>
            </ul>
          </div>
        <% end %>

        <div class="field">
          <%= form.label :title, "Post Title" %>
          <%= form.text_field :title, id: :post_title, autofocus: true, class: "required form-control" %>
        </div>

        <div class="field">
          <%= form.label :content %>
          <%= form.text_area :content, id: :content, rows: 6, placeholder: "Remember to practice good OPSEC. Unclassified content only!", class: "required form-control"  %>
        </div>

        <br>
        <div class="actions">
          <%= form.submit class: 'btn btn-block btn-outline-primary' %>
        </div>
      <% end %>

    </div><!--./col-->
  </div><!--./container-->
</div><!--./-->

Error I'm receiving in the development.log when I try and navigate to .../groups/:id/posts/new I'm not sure where the posts_path is coming from.

  Rendered posts/_form.html.erb (109.3ms)
  Rendered posts/new.html.erb within layouts/application (116.4ms)
Completed 500 Internal Server Error in 123ms (ActiveRecord: 0.4ms)



ActionView::Template::Error (undefined method `posts_path' for #<#<Class:0x000000049cf730>:0x00000004539ea0>):
    2:   <div class="container-fluid">
    3:     <div class="col-md-8 offset-md-2 col-sm-12 test">  
    4:   
    5:       <%= form_with(model: post, local: true) do |form| %>
    6:         <% if post.errors.any? %>
    7:           <div id="error_explanation">
    8:             <h2 class="text-danger"><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>

app/views/posts/_form.html.erb:5:in `_app_views_posts__form_html_erb__706299145122935431_35226660'
app/views/posts/new.html.erb:17:in `_app_views_posts_new_html_erb___3987427510426519105_36299580'

1 Answer 1

1

You defined post as a nested resource for groups.

<%= form_with(model: [group, post], ... %>

You should also pass the group to the partial.

<%= render 'form', post: @post, group: @group %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.