Muhambi
Muhambi

Reputation: 3522

Missing Template Rails on Update

I have a jQuery script that looks like:

$.ajax({
  url: "/books/"+ mId,
  type: 'PUT',
  dataType: 'json',
  data: { book: { description: bValue } },
}).done(function() {
  alert("DONE!");
});

In my routes, I have resources :books

In my book controller, I have the following:

def update
  @book = Book.find params[:id]
  respond_to do |format|
    if @book.update(book_params)
      format.json { render status: 200 }
    else
      format.json { render status: :unprocessable_entity }
    end
  end
end

But for some reason, I get the following error:

ActionView::MissingTemplate (Missing template books/update, application/update with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.

Upvotes: 3

Views: 1964

Answers (3)

Sami Birnbaum
Sami Birnbaum

Reputation: 831

This is what worked for me as of Rails 6

render plain: "OK"

Upvotes: 0

Niral Munjariya
Niral Munjariya

Reputation: 1371

If you want to return json message on 200 OK, you can simply do it like:

message = {'status' => 'Saved successfully.'}
render :json => message

Upvotes: 0

Amadan
Amadan

Reputation: 198294

If you just say render, you are rendering a view whose name is derived from the controller's and action's name. Such a view does not exist in your case, so you get an error.

An AJAX update should basically return no body, so you can just do

render :nothing => true, :status => 200

to avoid looking for the view.

Upvotes: 6

Related Questions