1

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

Community
  • 1
  • 1
Graham Slick
  • 6,692
  • 9
  • 51
  • 87
  • are you passing `format: :js` ? – dp7 Apr 13 '16 at 07:04
  • Could you check if the file webhook.js.erb is in app/views/pages please ? – Pholochtairze Apr 13 '16 at 07: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 Apr 13 '16 at 07:09
  • @dkp I tried: respond_to do |format| format: :js end but I get this error: syntax error, unexpected ':', expecting keyword_end – Graham Slick Apr 13 '16 at 07:09
  • @Pholochtairze it is – Graham Slick Apr 13 '16 at 07:10
  • @z.shan I just have this action, am I supposed to be able to access the page /webhook in my browser or should it only work with get/post requests ? – Graham Slick Apr 13 '16 at 07:12
  • @GrahamSlick how you are hitting this action ? there must be form `link_to` or something in your view where you have mentioned the path to this action. There you need to pass `format: :js`. It would be better if you could show your view code and server log. – dp7 Apr 13 '16 at 07:18
  • @dkp you're right I had to specify it in my link_to, but now it renders the js file, but doesn't execute it. How can I have it executed and not displayed ? Here is my link_to: <%= j link_to "Category", favorite_category_path(:id => category.id, :format => :js), method: :post %> – Graham Slick Apr 13 '16 at 07:28
  • @GrahamSlick please show your js file . – dp7 Apr 13 '16 at 07:34
  • @dkp I've edited my post – Graham Slick Apr 13 '16 at 07:39
  • try this one <%= j link_to "Category", favorite_category_path(:id => category.id), method: :post, remote: true %> – z.shan Apr 13 '16 at 07:47

2 Answers2

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
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
z.shan
  • 341
  • 1
  • 2
  • 13
2

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

<%= link_to "Category", favorite_category_path(:id => category.id), method: :post, remote: true %>
dp7
  • 6,651
  • 1
  • 18
  • 37
  • Thanks, it helped, but I still have an issue with the file being displayed instead of executed. I've edited my post – Graham Slick Apr 13 '16 at 07:39
  • @GrahamSlick are you getting any error in your log ? – dp7 Apr 13 '16 at 07:57
  • I get this : Rendered categories/webhook.js.erb (0.1ms) Completed 200 OK in 118ms (Views: 25.5ms | ActiveRecord: 24.2ms) Which is indeed the file I want, but I want it to be executed not rendered – Graham Slick Apr 13 '16 at 08:16
  • @GrahamSlick check my updated answer, you just need to add `remote: true` to make an ajax request which would execute your `js`. And, you do not need to `escape_javascript` or `j` before `link_to`. – dp7 Apr 13 '16 at 08:45