我有一个项目控制器,每个项目都有讨论,每个讨论都有评论。当我点击提交评论时,我收到此错误。 Missing template comments/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
我认为它与我的评论创建功能有关但我无法看到它。
comments_controller.rb
class CommentsController < ApplicationController
def new
@discussion = Discussion.find(params[:discussion_id])
@comment = Comment.new
end
def create
@discussion = Discussion.find(params[:discussion_id])
@comment = @discussion.comments.build(comment_params)
if @comment.save
new_discussion_comment_path(@discussion)
end
end
def comment_params
params.require(:comment).permit(:id, :body)
end
end
(评论)new.html.erb
<div class="container">
<div class="page-header">
<h1>Comments<small> Create a comment.</small></h1>
</div>
</div>
Discussion: <%= @discussion.title %> <%= link_to "Go back?", projects_path %>
<%= form_for [@discussion, @comment] do |f| %>
<div class="container">
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Submit comment", class: "btn btn-primary" %>
</div>
</div>
<% end %>
<!-- This is causing error. Once you add a column and figure it out it should work.
-->
<% if !@discussion.project.blank? %>
<% for item in @discussion.comments %>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<%= item.discussion %>
</div>
</div>
</div>
<% end %>
<% end %>
discussions_controller.rb
class DiscussionsController < ApplicationController
def new
@project = Project.find(params[:project_id])
@discussion = Discussion.new
end
def create
@project = Project.find(params[:project_id])
@discussion = @project.discussions.build(discussion_params)
if @discussion.save
redirect_to new_project_discussion_path(@project)
end
end
def discussion_params
params.require(:discussion).permit(:id, :title, :description)
end
end
(讨论)new.html.erb
<div class="container">
<div class="page-header">
<h1>Discussions<small> Discuss the project.</small></h1>
</div>
</div>
<%= form_for [@project, @discussion] do |f| %>
<div class="container">
Project: <%= @project.title %> <%= link_to "Go back?", projects_path %>
<hr>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Submit discussion", class: "btn btn-primary" %>
</div>
</div>
<% end %>
<% if !@project.discussions.blank? %>
<% for item in @project.discussions %>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<%= item.title %>
</div>
</div>
<div class="panel-body">
<p>
<%= item.description %> <br>
<%= link_to "Comment", new_discussion_comment_path(item) %>
</p>
</div>
</div>
<% end %>
<% end %>
_create_comments.rb
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :description
t.timestamps null: false
end
end
end
_create_nested_comment.rb
class NestedComment < ActiveRecord::Migration
def change
add_column :comments, :discussion_id, :integer
end
end
答案 0 :(得分:1)
您在redirect_to
CommentsController
行动中遗漏了create
。
尝试:
if @comment.save
redirect_to new_discussion_comment_path(@discussion)
end