Ajax:remote =>真的不行

时间:2018-06-11 07:37:41

标签: ruby-on-rails ajax

当我尝试向表单添加ajax时出现语法错误。我试图做到这一点,以便每次有人评论,它都不会刷新。我试过更改控制器并添加了format.js,但我仍然收到错误。

<%= simple_form_for ([post, post.comments.new], remote: true) do |f| %>

/app/views/posts/_post.html.erb:41: syntax error, unexpected ',', expecting ')' or ([post, post.comments.new], remote: true) do |f| @output_ ^
/app/views/posts/_post.html.erb:51: syntax error, unexpected keyword_ensure, expecting end-of-input ensure ^

查看档案

<div class="posts-wrapper">
    <div class="post">
        <div class="post-head">
            <div class="thumb-img"></div>
            <div class="user-name">
                <%= post.user.user_name %>
            </div>
        </div>
        <div class="image center-block">
            <%= link_to (image_tag post.image.url(:medium), class:'img-responsive'), post_path(post) %>
        </div>
        <div class="post-bottom">
            <div class="user-name-comment">
                <%= post.user.user_name %>
            </div>
            <div class="caption">
                <%= post.caption %>
            </div>
            <% if post.comments %>
                <% post.comments.each do |comment| %>
                    <div class="comment">
                        <div class="user-name">
                            <%= comment.user.user_name %>
                        </div>
                        <div class="comment-content">
                            <%= comment.content %>
                        </div>
                        <% if comment.user == current_user %>
                            <%= link_to post_comment_path(post, comment), method: :delete, data: { confirm: "Are you sure?" } do %>
                                <i class="fas fa-trash-alt fa-2x" style="padding: 5px; float: right; margin-top: -36px;"></i>
                            <% end %>
                        <% end %>
                    </div>
                <% end %>
            <% end %>
        </div>

        <div class="comment-like-form row">
            <div class="like-button col-sm-1">
              <i class="far fa-heart fa-2x"></i>
                <div class="comment-form col-sm-11">
                    <%= simple_form_for ([post, post.comments.new], remote: true) do |f| %>
                        <%= f.text_field :content, class: 'arthur_form', placeholder: 'Add a comment...' %>
                        <%= f.button :submit, class: "btn btn-primary padding: 50px;" %>
                    <% end %>
                </div>
            </div>
        </div>
    </div>
</div>

控制器评论

class CommentsController < ApplicationController
    before_action :set_post

    def create
        @comment = @post.comments.new(comment_params)
        @comment.user_id = current_user.id

        if @comment.save
            flash[:success] = "Comment succesfully created."
            respond_to do |format|
                format.html { redirect_to post_index_path }
                format.js
            end
        else
            flash[:alert] = "Check the comment form, something went horribly wrong."
            respond_to do |format|
                format.html { redirect_to post_index_path }
                format.js
            end
        end
    end


    def destroy
        @comment = @post.comments.find(params[:id])
        @comment.destroy
        flash[:alert] = "Comment Deleted."
        redirect_to post_index_path
    end

    private

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

    def set_post
      @post = Post.find(params[:post_id])
    end
end

控制器帖子

class PostsController < ApplicationController
    before_action :authenticate_user!
    before_action :owned_post, only: [:edit, :update, :destroy]

    def index
        @posts = Post.all
    end

    def new
        @post = current_user.posts.new
    end

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

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

        if @post.save
            flash[:success] = "Your post has been created!"
            respond_to do |format|
                format.html { redirect_to posts_path }
                format.js
            end
        else
            flash[:alert] = "Your new post couldn't be created!  Please check the form."
            render :new
        end
    end

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

    def update
        if @post = Post.find(params[:id])
            flash[:success] = "Post updated."
            @post.update(post_params)
            redirect_to(post_path(@post))
            flash.now[:alert] = "Update failed.  Please check the form."
        else
        end
    end

    def destroy
        @post = Post.find(params[:id])
        @post.destroy
        redirect_to posts_path
    end

    private

    def post_params
        params.require(:post).permit(:image, :caption)
    end

    def owned_post
        @post = Post.find(params[:id])
        unless current_user == @post.user
            flash[:alert] = "That post doesn't belong to you!"
            redirect_to post_index_path
        end
    end
end

1 个答案:

答案 0 :(得分:0)

使用以下命令更改表单初始化:

<%= simple_form_for [post, post.comments.new], remote: true do |f| %>

您不需要支架。