动作控制器::未知格式轨道 5.


Rails 5.2

在我的config/routes.rb中,我有:

post 'books_author', to: 'books#author'

当我运行耙子路线时,我得到:

books_author POST /books_author(.:format) books#author

在我的应用程序/控制器/books_controller.rb 中,我有:

def author
@books_collection = params[:books_collection]
@author_notes = params[:author_notes]
respond_to do |format| -----------------------------> this is line 27
format.js
end
end

在我的观点/书籍/索引.html.slim中,我有以下内容:

..........
td
= form_tag books_author_path do
= hidden_field_tag 'books_collection', books_collection
= hidden_field_tag 'author_notes', author_notes
= submit_tag 'Author Details'

当我单击"作者详细信息"链接时,出现以下错误:

ActionController::UnknownFormat in BooksController#author
ActionController::UnknownFormat
Extracted source (around line #27): (line 27 is highlighted in red)
@author_notes = params[:author_notes]
respond_to do |format|---------------------------------> this is line 27
format.js
end

有什么想法吗?

在你的控制器中,你告诉它返回JS作为响应,但你提供了.html.slim,这意味着它应该返回html或JS,你可以改变下面的行。

format.html

format.html {   }
format.js { }

或者我认为删除以下行是安全的,然后它会自动选择默认视图。

respond_to do |format| 
format.js
end

它将自动显示 html 版本。

最新更新