I'm building an Event app using Rails and have comments as a nested resource. I'm trying to implement an Edit function using Ajax/in-line editing but my Edit link isn't working. I'm not getting an error up on screen but I am getting above error from my development log. The comment 'id' = 27 relates to the event_id not the comment id. How do I rectify this? Here's the relevant code -
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 %>
I've checked the routes and they're all as they should be.I can't check whether the in-line editing will work unless I fix this first.
As far as I can see you have two before actions, set_event and then set_comment.
If you've set both of these to look for params[:id], then it means they will obviously both be using the same value for a search. You will need to change one of them, let us say set_event, to something like:
def set_event
@event = Event.find(params[:event_id])
end
You will also have to change your routes to account for the new id. If you are using your standard resource routes:
resources :events, param: event_id do
resources :comments
end
This will give you routes such as:
localhost:3000/events/:event_id/comments/:id
Which you can then use to find your events and comments.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.