最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - respond_to js "unknown format" - Stack Overflow

programmeradmin5浏览0评论

I have this action in my PagesController:

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

I get an error ActionController::UnknownFormat when trying to access /webhook.

I have a webhook.js.erb file in my PagesController, and a route associated:
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.

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:

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

I just have this in my webhook.js.erb to test it:

alert("h");

What I expect is that when I click on my link, an alert shows up on the page

I have this action in my PagesController:

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

I get an error ActionController::UnknownFormat when trying to access /webhook.

I have a webhook.js.erb file in my PagesController, and a route associated:
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.

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:

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

I just have this in my webhook.js.erb to test it:

alert("h");

What I expect is that when I click on my link, an alert shows up on the page

Share Improve this question edited May 23, 2017 at 12:31 CommunityBot 11 silver badge asked Apr 13, 2016 at 6:55 Graham SlickGraham Slick 6,87011 gold badges56 silver badges92 bronze badges 11
  • are you passing format: :js ? – dp7 Commented Apr 13, 2016 at 7:04
  • Could you check if the file webhook.js.erb is in app/views/pages please ? – Pholochtairze Commented Apr 13, 2016 at 7:06
  • 1 i think you are not calling js call, it is due to html call. can you share code how you are calling this action ? – z.shan Commented Apr 13, 2016 at 7:09
  • @dkp I tried: respond_to do |format| format: :js end but I get this error: syntax error, unexpected ':', expecting keyword_end – Graham Slick Commented Apr 13, 2016 at 7:09
  • @Pholochtairze it is – Graham Slick Commented Apr 13, 2016 at 7:10
 |  Show 6 more ments

2 Answers 2

Reset to default 2

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

You need to specify the remote: true in your link_to:

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

评论列表(0)

  1. 暂无评论