Javascript Rails 5.0-注释不起作用

Javascript Rails 5.0-注释不起作用,javascript,ruby-on-rails,ruby,ajax,model-view-controller,Javascript,Ruby On Rails,Ruby,Ajax,Model View Controller,我将注释作为事件应用程序的嵌套资源来实现,并且一个问题接着一个问题。起初,它工作得很好,但是,他们唯一的功能是创建和销毁。我想使用Ajax/remote:true为同一页面编辑添加一个编辑功能(以前从未这样做过),我遇到了麻烦。“编辑链接”不起作用/从未起过作用,现在甚至“创建”功能也不起作用。这就是开发日志上显示的内容- Processing by CommentsController#create as JS Parameters: {"utf8"=>"✓", "comment"=

我将注释作为事件应用程序的嵌套资源来实现,并且一个问题接着一个问题。起初,它工作得很好,但是,他们唯一的功能是创建和销毁。我想使用Ajax/remote:true为同一页面编辑添加一个编辑功能(以前从未这样做过),我遇到了麻烦。“编辑链接”不起作用/从未起过作用,现在甚至“创建”功能也不起作用。这就是开发日志上显示的内容-

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”问题不断涌现。这是我的密码-

评论\u 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
<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>
<%= 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 %>
$('#comment').append('<%= j render 'form' %>');
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'
路由没有意义-
“/events/27%2F32/comments/27/edit”
-注释id应该是32,事件id应该是27

routes.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
<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>
<%= 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 %>
$('#comment').append('<%= j render 'form' %>');
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'
改变

创建注释时不能设置注释

此外,如评论中所述,您的编辑链接应为

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

更改

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

创建注释时不能设置注释

此外,如评论中所述,您的编辑链接应为

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



在哪一行出现该错误?您能检查您的链接html以确保href?@Pavan中存在注释id吗?这是我单击“创建注释”时得到的。您的意思是在路线中?是的,肯定支持你去那里。我已经通过控制台查看过了,它显示在评论信息中,但我似乎无法通过参数获得它。@Mike.Whitehead不在控制台中,而是在您的网页中,由rails生成。(右键单击网页上的链接并选择“检查”,然后查看“href”属性中的内容)。或者只需查看鼠标在链接上时浏览器显示的URL。在哪一行出现此错误?您可以检查链接html以确保在href?@Pavan中存在注释id吗?这是我在路径中单击“创建注释”@HoloNorkaido时的注释id?是的,肯定支持你去那里。我已经通过控制台查看过了,它显示在评论信息中,但我似乎无法通过参数获得它。@Mike.Whitehead不在控制台中,而是在您的网页中,由rails生成。(右键单击网页上的链接并选择“检查”,然后查看“href”属性中的内容)。或者只看一下鼠标在链接上时浏览器显示的URL。我同意,但我该怎么做?要
编辑
,而不是
创建
,这是两种不同的操作,用途不同。在创建注释时,注释不存在,因此
id
nil
,因此
无法使用id=
@Mike.Whitehead找到。我向您展示了,从您唯一的数组中删除
:create
。@Md.FarhanMemon我已经这样做了,但编辑仍然不起作用。你能明白为什么会发生这种情况吗?@Mike.Whitehead你的链接是错误的。
应该是
或者
我同意,但是我该怎么做呢?要
编辑
,而不是
创建
,这是两种不同的操作,有着不同的目的。在创建注释时,注释不存在,因此
id
nil
,因此
无法使用id=
@Mike.Whitehead找到。我向您展示了,从您唯一的数组中删除
:create
。@Md.FarhanMemon我已经这样做了,但编辑仍然不起作用。你能明白为什么会发生这种情况吗?@Mike.Whitehead你的链接是错误的。
应该是