Respond_To Js "Unknown Format"

Respond_To Js "Unknown Format"

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

11

2 Answers

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 %>
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.