如何在 Rails 6 中使用 js.erb 视图 [英] How to use js.erb views in Rails 6
问题描述
是否有可能在 Rails 6 应用程序中使用 js.erb
视图?我明白了
Is there possibility to use js.erb
views in Rails 6 application? I get
MyController#my_action is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
迁移到 rails 6 后使用 js.erb
视图的操作出错.
error for actions using js.erb
views after migration to rails 6.
在我的操作中,我只是为实例变量赋值:
In my action I just assign value to instance variable:
def next_question
@lesson = lesson
end
在我有的路线
put ':package_id/lessons/next_question', to: 'lessons#next_question', as: 'next_question'
并且有提交此请求的表格
And there is form for submit this request
= form_tag next_question_path(@package), method: :put, remote: true
如果我创建(用于测试)html.slim
文件,一切正常.如果我将 format.js
添加到控制器,我会得到 ActionController::UnknownFormat
If I create (for test) html.slim
file, everything works well.
If I add format.js
to controller, I get ActionController::UnknownFormat
推荐答案
创建一个视图文件next_question.js.erb
,然后做这个
Create a view file next_question.js.erb
, then do this
def next_question
@lesson = lesson
respond_to do |format|
format.js
end
end
另外,在application.js
文件中启用ujs
,添加这一行
Also, enable ujs
in application.js
file, add this line
require("@rails/ujs").start()
试试看!
这篇关于如何在 Rails 6 中使用 js.erb 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!