0
votes

I'm building an Events app using Rails and have a comments section at the foot of the Event show page. I want the user to be able to create/update(edit)/delete only their own comments whilst remaining on the same show page. How do I do this?

I've put some code together but I'm fairly new to rails and my code tries to take the user away from the show page and create a 'comments' show page rather than just edit the form on the events show page. I have the correct model associations has_many/belongs_to and my comments are nested in my events routes. Here's my code so far -

Comments_controller.rb

   class CommentsController < ApplicationController


def create
    @event = Event.find(params[:event_id])
    @comment = @event.comments.create(params[:comment].permit(:user_id, :body))


    redirect_to event_path(@event)
end

def show
    @comment = Comment.find(params[:id])
    @comment = @event.comments.find(params[:id])
end

def edit
    @comment.user = current_user

end

def update
    if @comment.update(comment_params)
        redirect_to event_path(@event)
    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 comment_params
        params.require(:comment).permit(:body, :event_id, :user_id)
    end
end

Event.show.erb

  # some code for Events show...

    # Comments code - 
                <% if user_signed_in?  %>

                <div id="comments">
                    <%= render 'comments/form', commentable: @event %>
                    <% if @event.comments.any? %>
                        <h2><%= @event.comments.size %> Comment</h2>
                        <%= render @event.comments %>
                    <% else %>
                    <h2>There are no comments yet</h2>
                <% end %>

                </div>  

                <% end %>

Comments._comment.html.erb

  <div class="comment clearfix">


 <div class="comment_content">
    <p class="comment_user"><strong><%= comment.user %></strong></p>
    <p class="comment_body"><%= comment.body %></p>
    <p class="comment_time"><%= time_ago_in_words(comment.created_at) %> Ago</p>
  </div>

  <% if user_signed_in? and current_user %>
    <p><%= link_to 'Delete', [comment.event, comment],
                                  method: :delete,
                                  class: "button",
                                    data: { confirm: 'Are you sure?' } %></p>
    <p><%= link_to 'Edit', [comment.event, comment] %> </p>                                 
 <% end %>

</div>

comments_form.html.erb

  <%= simple_form_for([commentable, Comment.new ]) do |f| %>

<%= f.label :comment, label:  'Add a comment' %><br> 
<%= f.text_area :body %><br>
<br>
<%= f.button :submit, "Create", class: "btn btn-primary" %>
<% end %>
1
where is the code of comments/form ? - jithya
I won't help you any more. Last time I spend a lot of time on you and you said it was useful to you, but 0 upvote or marked as correct! - Fallenhero
On which question are you referring? - Mike.Whitehead

1 Answers

0
votes

to perform the actions while staying on the same page you need to add remote: true to your form and submit via JS. You can read more about this in the Rails/JS Docs. http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html Than, the standard rails way is to lead you to another route. To prohibit that, and perform everything on the same view page you can use JS.

To limit the actions to the current user you need to make sure that the comment belongs to current_user. so sth like if user_signed_in? and comment.user_id == current_user.id (Just in case your code above is not bullet proof. )