2016-07-15 85 views
1

如何使「創建」動作只調用js.erb文件,而不是重新加載找到缺少模板的頁面?遠程導軌形式(simple_form_for)

我試圖創建遠程形式的書對象,但行動 '創造' 在這樣的錯誤的結果:

Missing template books/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :axlsx, :haml, :rabl]}.

書籍/ _form.haml(庫:@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 

本書籍/ create.js.erb:

$('#books_table').replaceWith('<%= j render "table", books: @books %>'); 

UPDATE:

我換書/ create.js.erb:

<% if [email protected]? %> 
    alert("error!") 
<% else %> 
    $('#books_table').replaceWith('<%= j render "table", books: @books %>'); 
<% end %> 

在這種情況下,當@book無效,js文件被稱爲罰款,但萬一它可以保存 - 仍然有關於丟失模板的錯誤

回答

1

remote: true選項是n不被接受,因爲您可以在錯誤消息中看到:formats=>[:html],而不是按預期看到:formats=>[:js]

您確定您有jquery_ujs在您的資產管道中正確設置?這是選擇remote: true選項,並導致表單通過JS而不是默認的HTML格式提交。

檢查/app/assets/javascript/application.js文件

你需要有:

//= require jquery //= require jquery_ujs

在鏈輪清單

+0

這兩個項目(jquery&jquery_ujs)已包含在application.js – Zelenka

+0

請查看我更新的問題 – Zelenka