我是Ruby和Web开发的新手。 我有很多按钮,我想做同样的功能,但有不同的参数。 但是,为了测试它,我想做的就是在我的控制器中调用一个非常简单的函数(称为gallery_matches_controller.rb)
在index.html.erb中:
<%= button_to "Confirm", gallery_matches_path, action: "pleaseWork"%>
在routes.rb中:
match 'gallery_matches/pleaseWork', to: 'gallery_matches#pleaseWork', via: [:get, :post]
在gallery_matches_controller.rb
中def pleaseWork
puts "hello"
end
我收到此错误:ActionView :: MissingTemplate 我假设我得到它,因为我的routes.rb行出了点问题,但我无法理解。我试图关注人们的例子,但似乎都没有。
答案 0 :(得分:0)
而不是
puts "hello"
尝试
render text: "hello"
错误消息显示缺少模板,因为如果您没有告诉它输出其他内容,Rails会自动尝试渲染(输出)与控制器和操作相同的模板。
所以在这种情况下,它希望自动呈现像gallery_matches/pleaseWork.html.erb
这样的模板。如果你明确地告诉它渲染一些文本,它会改为而不是寻找模板。
在旁注中,please_work
比pleaseWork
更常规。 Rails对命名做了一些假设(“约定优于配置”),因此如果不遵循标准命名,可能会遇到问题。
另外,我不认为
<%= button_to "Confirm", gallery_matches_path, action: "pleaseWork"%>
将路由到预期的控制器。你可以做到
<%= button_to "Confirm", controller: "gallery_matches", action: "pleaseWork"%>
代替。