简体   繁体   English

response_to js“未知格式”

[英]respond_to js “unknown format”

I have this action in my PagesController: 我在PagesController中执行以下操作:

def webhook
  respond_to do |format|
    format.js
  end
end

I get an error ActionController::UnknownFormat when trying to access /webhook. 尝试访问/ webhook时收到错误ActionController::UnknownFormat

I have a webhook.js.erb file in my PagesController, and a route associated: 我的PagesController中有一个webhook.js.erb文件,并且有一个关联的路由:
get '/webhook', to: 'pages#webhook'

I checked these posts: Unknown format in rails 4 and Rails Unknown format error (both HTML and JS) but none had a working answer. 我检查了这些帖子: Rails 4Rails Unknown format错误(HTML和JS)中的未知格式,但是没有一个有效的答案。

But I used this syntax previously and it worked so I have no idea what's going on. 但是我以前使用过这种语法,并且可以正常工作,所以我不知道发生了什么。

How could I fix this ? 我该如何解决?

EDIT: Now, I've added :format => :js to my link_to, and when I click, the browser displays the js file webhook.js.erb Here is my link_to: 编辑:现在,我已经将:format => :js到我的link_to,并且当我单击时,浏览器将显示js文件webhook.js.erb,这是我的link_to:

 <%= j link_to "Category", webhook_path(:format => :js), method: :post %> 

I just have this in my webhook.js.erb to test it: 我只是在我的webhook.js.erb中对此进行了测试:

alert("h");

What I expect is that when I click on my link, an alert shows up on the page 我期望的是,当我单击链接时,页面上会显示一条警报

Try: 尝试:

<%= j link_to "Category", favorite_category_path(id: category.id), method: :post, remote: true %>

And your controller: 和您的控制器:

respond_to do |format|
  format.js
end

您需要在link_to指定remote: true

<%= link_to "Category", favorite_category_path(:id => category.id), method: :post, remote: true %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果存在format.html,则response_to无法与format.js一起使用 - respond_to not working with format.js if format.html is present respond_to 做 |格式| format.js 在 Rails 6.1.3 中不起作用 - respond_to do |format| format.js not working in rails 6.1.3 使用response_to format.js替换Rails上textarea的内容 - using respond_to format.js to replace the content of a textarea on rails Rails 中的 response_to 出现未知格式错误。 正确尝试实施轮询更新 - Unknown Format error with respond_to in Rails. Correctly trying to implementing polling-to-update response_to块中的Format.js无法以条件格式运行 - Format.js in respond_to block doesn't work in an conditional format “respond_to do | format |”抛出UnknownFormat错误 - “respond_to do |format|” throws UnknownFormat error 为什么没有调用正确的“ respond_to”格式? - Why is the proper “respond_to” format not getting called? Rails:make(link_to“示例”,“#”,远程:true)绕过(respond_to…format.js) - Rails: make (link_to “example”, “#”, remote: true) bypass (respond_to … format.js) Rails 3.1:使用&#39;respond_to ... format.js&#39;而不使用&#39;page&#39;变量的新方法 - Rails 3.1: The new way to use 'respond_to … format.js' without the 'page' variable 没有respond_to就调用.js文件 - .js file being called without respond_to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM