ActionController :: UnknownFormat в комментариях - PullRequest
0 голосов
/ 10 апреля 2019

Я пытаюсь создать приложение управления библиотекой и застрял в модели комментариев

Итак, это мое отношение к модели комментариев:

    class User < ApplicationRecord
       has_many :comments, dependent: :destroy
    end

    class Book < ApplicationRecord
       has_many :comments, dependent: :destroy
    end

    class Comment < ApplicationRecord
       belongs_to :user
       belongs_to :book
       validates :content, presence: true, allow_blank: false
    end

comments_controller.rb

    class CommentsController < ApplicationController
      before_action :load_book, only: :create

    def index
      @q = Comment.ransack(params[:q]) 
  @comments = @q.result.page(params[:page])
    end

    def new;
    end 

    def create   
     @comment = Comment.new(comment_params)
     @comment.user_id = current_user.id
     @comment.book_id = params[:book_id]

     if @comment.save
     flash[:success] = "create_success"
     redirect_to book_path(@book)

    end

    private
    def comment_params
       params.require(:comment).permit(:user_id, :book_id, :content)
    end

    def load_book
      @book = Book.find_by(params[:id])
      return if @book
      flash[:danger] = "books.load_book.error_message"
      redirect_to root_path
     end
     end

в books_controller.rb

    def show
        @comment = Comment.new
end

app / view / books / show.html.erb

    <% provide :title, @book.name %>
    <h1><%= @book.name %></h1>

    <div class="container">
    <dl>
    <dt>Author:</dt><dd><%= link_to @book.author.name, @book.author 
    %></dd><br>
    <dt>Category:</dt><dd>
    <% @book.categories.each do |c|%>
          <%= link_to c.name, c%> |
    <% end %>
    <dt>Publisher:</dt><dd><%= @book.publisher %></dd><br>
    <dt>Page:</dt><dd><%= @book.page %></dd><br>
    <dt>Quantity:</dt><dd><%= @book.quantity %></dd><br>

    </dl>
    <aside>
      <section>
       <%= render 'comments/form' %>  
      </section>
    </aside>
    </div>

app / view / comments / _form.html.erb

    <h3>post_comment</h3>
    <p>
     <%= form_for(@comment) do |f| %>
      <%= f.text_area :content %>
      <%= f.submit "post", class: "btn btn-primary" %>
     <% end %>
    </p>

И браузер показывает эту ошибку:

    Missing template comments/new, application/new with {:locale=> 
    [:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, 
    :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * 
    "/home/default/ProjectRails/app/views"

Это первый раз, когда я устанавливаю отношения многие ко многим, это нормально, или проблема в другой части?

1 Ответ

2 голосов
/ 10 апреля 2019

Как четко указано в сообщении об ошибке

CommentsController#index is missing a template for this request format and variant.

Вам необходимо создать файл app/view/comments/index.html.erb с некоторым содержанием

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...