Reputation: 1527
How to make a 'create' action call only js.erb file and not reload page finding a missing template?
I'm trying to create a book object with remote form, but the action 'create' results in such error:
Missing template books/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :axlsx, :haml, :rabl]}.
books/_form.haml (library: @library):
= simple_form_for @book, url: library_books_path(library), remote: true do |f|
books_controller.rb:
def index
@books = @library.books
@book = Book.new
end
def create
@book = @library.books.create(book_params)
end
def destroy
@book = Book.find(params[:book_id])
@book.destroy
end
books/create.js.erb:
$('#books_table').replaceWith('<%= j render "table", books: @books %>');
UPDATE:
I changed books/create.js.erb:
<% if [email protected]? %>
alert("error!")
<% else %>
$('#books_table').replaceWith('<%= j render "table", books: @books %>');
<% end %>
In this case, when @book
is not valid, js file is called fine, but in case it can be saved - there's still error about missing template
Upvotes: 1
Views: 1411
Reputation: 2986
The remote: true
option is not being picked up, because you can see :formats=>[:html]
in your error message, NOT :formats=>[:js]
as expected.
Are you sure you have jquery_ujs set up properly in your asset pipeline? This is what picks up the remote: true
option, and causes the form to submit via JS rather than the default HTML format.
Check your /app/assets/javascript/application.js
file
You need to have:
//= require jquery
//= require jquery_ujs
in your Sprockets manifest.
Upvotes: 1