All threads / Rails 5.0 - How to implement in-place editing without using best_in_place gem
Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Mike Whitehead asked in Rails

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 -

_comment.html.erb

<% 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 }
        else
            format.html { render :new }
            format.json { render json: @comment.errors, status: :unprocessable_entity }
        end
    end
end

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.

That's incredible! I have never come across this feature/capability in any of my search results.

For my present use, the Bootstrap input is more evident for the less informed computer user, as well as being more pleasing to the eye. As a result, dynaspan appeals to me more visually. In terms of prospective features for this gem in the near future, I'll keep your proposal in mind.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 66,029+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.