Ich baue eine Event-App mit Rails und haben Kommentare als verschachtelte Ressource. Ich versuche, eine Edit-Funktion mit Ajax/Inline-Bearbeitung zu implementieren, aber mein Edit-Link funktioniert nicht. Ich bekomme keinen Fehler auf dem Bildschirm, aber ich bekomme einen Fehler von meinem Entwicklungsprotokoll. Der Kommentar 'id' = 27 bezieht sich auf die event_id nicht auf die Kommentar-ID. Wie behebe ich das? Hier ist der entsprechende Code -Rails - ActiveRecord :: RecordNotFound (Konnte nicht mit 'ID' = 27) finden:
comments_controller.rb
class CommentsController < ApplicationController
before_action :set_event, only: [:show, :create, :edit, :update, :destroy]
before_action :set_comment, only: [:show, :create, :edit, :update, :destroy]
def create
@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
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
@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>
<% if user_signed_in? %>
<p><%= link_to 'Edit', edit_event_comment_path(@event, @comment.event), id: "comment", remote: true %></p>
<p><%= link_to 'Delete', [@comment.event, comment],
method: :delete,
class: "button",
data: { confirm: 'Are you sure?' } %></p>
<% end %>
</div>
</div>
edit.js.erb
$('#comment').append('<%= j render 'form' %>');
_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 %>
Ich habe die Routen geprüft und sie sind alle wie sie, ob die in-line Bearbeitung wird funktionieren, wenn ich diese erste beheben kann nicht überprüfen be.I sollte .
warum das? 'edit_event_comment_path (@event, @ comment.event)'? beide übergeben Ereignisse, tun 'edit_event_comment_path (@event, @comment)' –
Dann bekomme ich 'fehlende erforderliche Schlüssel [: id]' Fehler auf der Seite. –
ist es '@ comment' oder nur' comment'? Ich sehe es als "Kommentar" in Ihrer Sicht, verwenden Sie es –