Mike.Whitehead
Mike.Whitehead

Reputation: 818

Rails - How to implement :remote => true, to edit comments on the same page

I'm building an events site using Rails 5. On the Event#show page I have a comments section which I want to give edit/update functionality to without the user leaving the page. I'm trying to use :remote => true in the form but I'm hitting errors, here's the views and controller code -

comments/_form.html.erb

<%= simple_form_for([@event, @event.comments.build, :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 %>

Comments_controller.rb

def update

    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

The current error I'm getting is 'undefined method `model_name' for {:remote=>true}:Hash'. This is pointing to the events#show method in the Events controller. I'm not sure I'm using :remote => true in the correct place hence the error. This is the first time I've done this so I'm hitting errors each step of the way. Any help/assistance would be appreciated.

Upvotes: 0

Views: 409

Answers (1)

Deepak Mahakale
Deepak Mahakale

Reputation: 23671

You are not using proper format of simple_form_for. It should be

<%= simple_form_for([@event, @event.comments.build], remote: true ) do |f| %>

Upvotes: 1

Related Questions