ActionView :: Template :: Error (отсутствует блок): при использовании simple_form_for - PullRequest
0 голосов
/ 05 января 2019

Я создал страницу с сообщениями, и если я открываю какой-то конкретный пост, скажите «localhost: 3000 / newsfeeds / 14». Я хочу сделать комментарии и форму комментариев в этом тоже. Мои маршруты:

   newsfeed_comments GET    /newsfeeds/:newsfeed_id/comments(.:format)          comments#index
                     POST   /newsfeeds/:newsfeed_id/comments(.:format)          comments#create
new_newsfeed_comment GET    /newsfeeds/:newsfeed_id/comments/new(.:format)      comments#new
edit_newsfeed_comment GET    /newsfeeds/:newsfeed_id/comments/:id/edit(.:format) comments#edit
    newsfeed_comment GET    /newsfeeds/:newsfeed_id/comments/:id(.:format)      comments#show
                     PATCH  /newsfeeds/:newsfeed_id/comments/:id(.:format)      comments#update
                     PUT    /newsfeeds/:newsfeed_id/comments/:id(.:format)      comments#update
                     DELETE /newsfeeds/:newsfeed_id/comments/:id(.:format)      comments#destroy
           newsfeeds GET    /newsfeeds(.:format)                                newsfeeds#index
                     POST   /newsfeeds(.:format)                                newsfeeds#create
        new_newsfeed GET    /newsfeeds/new(.:format)                            newsfeeds#new
       edit_newsfeed GET    /newsfeeds/:id/edit(.:format)                       newsfeeds#edit
            newsfeed GET    /newsfeeds/:id(.:format)                            newsfeeds#show
                     PATCH  /newsfeeds/:id(.:format)                            newsfeeds#update
                     PUT    /newsfeeds/:id(.:format)                            newsfeeds#update
                     DELETE /newsfeeds/:id(.:format)                            newsfeeds#destroy
                root GET    /                                                   newsfeeds#index

Это КомментарииКонтроллер:

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(comment_params)
    @comment.user_id = current_user.id

    if @comment.save
        redirect_to newsfeed_path
    else
        render 'new'
    end
end

private

def comment_params
    params.require(:comment).permit(:content)
end
end

Вот контроллер новостей:

class NewsfeedsController < ApplicationController

before_action :find_post, only: [:show, :destroy, :edit, :update]

def index
    @posts = Post.all.order("created_at DESC")
end

def show
    # before_action is taking care of all 4 i.e(sho,edit,update and destroy)..Keeping it DRY
end

def new
    @post = current_user.posts.build
end

def create
    @post = current_user.posts.build(post_params)

    if @post.save
        redirect_to root_path
    else
        render 'new'
    end
end

def edit
end

def update
    if @post.update(post_params)
        redirect_to newsfeed_path(@post)
    else
        render 'edit'
    end
end

def destroy
    @post.destroy
    redirect_to root_path
end

private

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

def find_post
    @post = Post.find(params[:id])

end

конец

в _form.html

<%= simple_form_for ([@post, @post.comments.build]), url: newsfeed_comments_path(@post.id) do |f| %>
  <%= f.input :content,label: false, placeholder: "write your comment here..." %>
  <%= f.button :submit, :class => 'btn-custom' %>
<% end %>

Я получаю эту ошибку:

ActionView::Template::Error (Missing block): 1: <%= simple_form_for ([@post, 
@post.comments.build]), url: newsfeed_comments_path(@post.id) do |f| %> 2: 
<%= f.input :content,label: false, placeholder: "write your comment here..." 
%> 3: <%= f.button :submit, :class => 'btn-custom' %> 4: <% end %>

Спасибо за поддержку заранее.

Вот show.html, где отображается _form,

<div class="col-md-10 col-md-offset-1">

    <div>

        <h3><%= @post.content %></h3>
        <p class=""><%= time_ago_in_words(@post.created_at) %> ago </p>

    </div>

    <div>
        <h4>Comments:</h4>
        <p><%= render @post.comments %></p>

        <h4 class="reply-to-msg">Leave your Comment here..</h4>
        <%= render 'comments/form' %>

    </div>

    <div>
        <%= link_to "Back", root_path, class: "btn btn-default" %>
        <%= link_to "Edit", edit_newsfeed_path, class: "btn btn-primary" %>
        <%= link_to "Delete", newsfeed_path, method: :delete, data: { confirm: "You Sure Master..?"} , class: "btn btn-danger" %>
    </div>

</div>

Маршруты здесь:

devise_for :users
resources :newsfeeds do
  resources :comments
end
root 'newsfeeds#index'

1 Ответ

0 голосов
/ 05 января 2019

Это вопрос скобок. У вас есть пробел между именем метода и скобками. Должно быть

<%= simple_form_for [@post, @post.comments.build], url: newsfeed_comments_path(@post.id) do |f| %>

или

<%= simple_form_for([@post, @post.comments.build], url: newsfeed_comments_path(@post.id)) do |f| %>
...