Rails 5.0 - 评论不起作用

时间:2017-05-24 11:12:37

标签: javascript ruby-on-rails ruby ajax model-view-controller

我正在将评论作为事件应用程序的嵌套资源实现,并且一个接一个地遇到问题。最初它工作得很好,然而,他们唯一的功能是创建&破坏。我想使用Ajax / remote添加一个编辑功能:对于同一页面编辑是真的(以前从未做过)而且我已经碰壁了。编辑link_to没有/从未工作,现在甚至创建功能都不起作用。这就是开发日志中的内容 -

Processing by CommentsController#create as JS
  Parameters: {"utf8"=>"✓", "comment"=>{"body"=>"Comment."}, "commit"=>"Create Comment", "event_id"=>"27"}
  [1m[36mComment Load (0.1ms)[0m  [1m[34mSELECT  "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT ?[0m  [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 1ms (ActiveRecord: 0.1ms)



ActiveRecord::RecordNotFound (Couldn't find Comment with 'id'=):

我通过反复试验尝试了各种不同的参数,但'id'问题不断涌现。这是我的代码 -

comments_controller.rb

class CommentsController < ApplicationController


    before_action :set_comment, only: [:show, :create, :edit, :update, :destroy]


    def create
        @event = Event.find(params[:event_id])
        @comment = @event.comments.create(comment_params)
        @comment.user_id = current_user.id

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


    # GET /comments/1/edit
    def edit
        @event = @comment.event
        @comment = @event.comments.find(params[:id])
        respond_to do |f|
            f.js 
            f.html 
        end
    end

    def show
    end



    def update 
        if @comment.update(comment_params)
            redirect_to @event, notice: "Comment was successfully updated!"
        else
            render 'edit'
        end
    end



    def destroy
        @event = Event.find(params[:event_id])
        @comment = @event.comments.find(params[:id])
        @comment.destroy

        redirect_to event_path(@event)
    end

    private

    def set_comment
      @comment = Comment.find(params[:id])
    end

    def set_event
        @event = Event.find(params[:event_id])
    end


    def comment_params
      params.require(:comment).permit(:name, :body)
    end


end

_comment.html.erb

<div class="comment clearfix">
  <div class="comment_content">
    <div id="<%=dom_id(comment)%>" class="comment">  
      <p class="comment_name"><strong><%= comment.name %></strong></p>
      <p class="comment_body"><%= comment.body %></p>

  </div>

      <p><%= link_to 'Edit', edit_event_comment_path([comment.event, comment]), id: "comment", remote: true %></p>
                  <p><%= link_to 'Delete', [comment.event, comment],
                                     method: :delete,
                                        class: "button",
                              data: { confirm: 'Are you sure?' } %></p>
    </div>
</div>

_form.html.erb

<%= simple_form_for([@event, @comment], remote: true) do |f| %>


    <%= f.label :comment %><br>
    <%= f.text_area :body %><br>
    <br>
    <%= f.button :submit, label: 'Add Comment', class: "btn btn-primary" %>
<% end %>

edit.js.erb

$('#comment').append('<%= j render 'form' %>');

我认为我对这件事的'id'以及如何在页面上运行remote: true功能感到困惑。我不想接受失败,但如果我不努力,我可能不得不接受失败。

更新 -

当我尝试编辑现有评论时,我在开发日志中得到了这个 -

Started GET "/events/27%2F32/comments/27/edit" for ::1 at 2017-05-24 12:28:20 +0100
Processing by CommentsController#edit as JS
  Parameters: {"event_id"=>"27/32", "id"=>"27"}
  [1m[36mComment Load (0.1ms)[0m  [1m[34mSELECT  "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT ?[0m  [["id", 27], ["LIMIT", 1]]
Completed 404 Not Found in 1ms (ActiveRecord: 0.1ms)




ActiveRecord::RecordNotFound (Couldn't find Comment with 'id'=27):

路线没有意义 - "/events/27%2F32/comments/27/edit" - 评论ID应为32,事件编号为27。

的routes.rb

Rails.application.routes.draw do

     devise_for :users, :controllers => { omniauth_callbacks: "omniauth_callbacks", registrations: "registrations" }  


  resources :users
  # the above resource draws out routes for user profiles
  resources :events do

    resources :comments
    resources :bookings

  end





    root 'events#index'

1 个答案:

答案 0 :(得分:4)

变化

before_action :set_comment, only: [:show, :create, :edit, :update, :destroy]

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

创建时无法设置注释。

另外,正如评论中所讨论的,您的编辑链接应为

<%= link_to 'Edit', [comment.event, comment], id: "comment", remote: true %>