Rails:嵌套表单问题

时间:2018-02-25 20:54:06

标签: ruby-on-rails ruby-on-rails-5

我正在尝试将“帖子”嵌套在“群组”下,以便当我有一个群组页面时,我可以拥有与该特定群组相关联的帖子。

我现在遇到一个问题,就是试图让“新”模板为帖子呈现。

这里是我的路线

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

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

发布模型 (组模型具有:has_many :posts, dependent: :destroy

class Post < ApplicationRecord
  belongs_to :group

  validates :title, :content, presence: true

end

帖子/新模板

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

帖子/ _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><!--./-->

当我尝试导航到... / groups /:id / posts / new时,我在development.log中收到错误我不确定posts_path的来源。

  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'

0 个答案:

没有答案