ActionView :: Template :: Error(缺少块):在使用simple_form_for时

时间:2019-01-05 19:23:36

标签: ruby-on-rails ruby nested simple-form model-associations

我创建了一个显示帖子的页面,如果我打开任何特定的帖子,请说“ 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

这是CommentController:

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

这里是NewsfeedsController:

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 :(得分:1)

这是一个括号问题。方法名称和方括号之间有一个空格。应该是

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