Ruby on rails 远程rails表单(简单表单)
如何仅对js.erb文件进行“创建”操作调用,而不重新加载页面以查找缺少的模板? 我正在尝试使用远程窗体创建book对象,但“创建”操作会导致以下错误:Ruby on rails 远程rails表单(简单表单),ruby-on-rails,simple-form,Ruby On Rails,Simple Form,如何仅对js.erb文件进行“创建”操作调用,而不重新加载页面以查找缺少的模板? 我正在尝试使用远程窗体创建book对象,但“创建”操作会导致以下错误: 缺少使用{:locale=>[:en],:formats=>[:html],:variants=>[],:handlers=>[:erb,:builder,:raw,:ruby,:coffee,:jbuilder,:axlsx,:haml,:rabl]}的模板书/创建,应用程序/创建。 图书/_form.haml(图书馆:@library):
缺少使用{:locale=>[:en],:formats=>[:html],:variants=>[],:handlers=>[:erb,:builder,:raw,:ruby,:coffee,:jbuilder,:axlsx,:haml,:rabl]}的模板书/创建,应用程序/创建。
图书/_form.haml(图书馆:@library):
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 %>');
<% if !@book.valid? %>
alert("error!")
<% else %>
$('#books_table').replaceWith('<%= j render "table", books: @books %>');
<% end %>
$(“#图书_表”)。替换为(”;
更新:
我更改了books/create.js.erb:
$('#books_table').replaceWith('<%= j render "table", books: @books %>');
<% if !@book.valid? %>
alert("error!")
<% else %>
$('#books_table').replaceWith('<%= j render "table", books: @books %>');
<% end %>
警报(“错误!”)
$('books_table')。替换为('';
在这种情况下,当
@book
无效时,js文件将被调用为fine,但如果可以保存,则仍然会出现缺少模板的错误远程:true选项未被选中,因为您可以在错误消息中看到:formats=>[:html]
,而不是预期的:formats=>[:js]
您确定在您的资产管道中正确设置了jquery\u ujs?这会选择remote:true
选项,并使表单通过JS而不是默认的HTML格式提交
检查您的/app/assets/javascript/application.js
文件
您需要:
/=需要jquery
//=需要jquery\u ujs
在链轮清单中。这两个项目(jquery和jquery_ujs)都已包含在application.js中