Skip to main content

Rails 5.0 - How to implement in-place editing without using best_in_place gem

Rails • Asked by Mike Whitehead

I'm building an Events site using RoR which I've just upgraded to v5.0.1 (not moving to 5.1 just yet). My events show page has a comments section at the foot of the page which, so far, only has a create and delete functionality.
I want to add an Edit/Update function to comments so only the user who created the comment can edit the comment if they see fit. I want them to be able to edit their comment whilst on the same page. I don't want to use best_in_place gem as this appears to be on the verge of being dormant having not been updated for 3 years.

I'm trying to implement these changes using remote: true & Ajax but I've never done this before and there doesn't appear to be a clear guide via the internet/SO. Here's my views & controller code -


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

*Comments_controller.rb *

def update
    @comment.user = current_user

    respond_to do |format|
        if @comment.update
            format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
            format.js   { }
            format.json { render :show, status: :created, location: @comment }
            format.html { render :new }
            format.json { render json: @comment.errors, status: :unprocessable_entity }

My understanding is that I need to include a .js.erb file in my views/comments folder to deal with this ( edit.js.erb ?) but what I'm not clear on is exactly what javascript code I need to include in order for this to work. Also, I don't think my controller code above seems right - should this go in the Edit action? Do I also need an Event#show action in my events controller as this is where it sits in the views?

Any assistance would be greatly appreciated.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.