roguerat
roguerat

Reputation: 217

Rails: Template is missing

I have a projects controller, each project has discussions and each discussion has comments. When I click submit comment I get this error. Missing template comments/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. I thought it had something to do with my comments create function but I can't see it.

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

(comments) 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 [email protected]? %>
  <% 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

(discussions) 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 [email protected]? %>
  <% 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

Upvotes: 0

Views: 35

Answers (1)

Jordan Allan
Jordan Allan

Reputation: 4486

You're missing the redirect_to in your CommentsController create action.

Try:

if @comment.save
  redirect_to new_discussion_comment_path(@discussion)
end

Upvotes: 1

Related Questions