0

I have a problem when I click a button to run some js file, rails throws an error saying: ActionController::UnknownFormat. and respond_to do |format| is highlighted

I have these 3 file paths

views -> file -> file1 -> create.js.erb
controllers -> file -> file1_controller.rb

views -> file -> files2 -> file2.html.erb

Inside file2.html.erb there is a button that says:

<%= button_to "Go To", 
  file_file1_create_path,
  params: {id: table.id},
  remote: true,
  class:"btn btn-outline-success" 
%>

Inside file1_controller.rb it says:

class File::File1Controller < FileController
  def create
    # a lot if code
    respond_to do |format|
      format.js
    end
  end
end

The request logs are:

Started GET "/file/..." for 127.0.0.1 at 2022-08-21 19:37:00 -0400

ActionController::RoutingError (No route matches [GET] "/file/..."):

Started POST "/file/file1/create" for 127.0.0.1 at 2022-08-21 19:37:24 -0400
Processing by File::File1Controller#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "id"=>"1"}
  Item Load (0.5ms)  SELECT "items".* FROM "items" WHERE "items"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/file/file1_controller.rb:5:in `create'
Completed 406 Not Acceptable in 878ms (ActiveRecord: 0.9ms | Allocations: 6099)



ActionController::UnknownFormat (ActionController::UnknownFormat):

app/controllers/file/file1_controller.rb:23:in `create'
Ese10
  • 17
  • 6
  • you need to add **format.html** inside respond_to block (same as format.js). – Abhishek Jadav Aug 21 '22 at 15:05
  • Also, respond_to state where do you want application to redirect to. In our case if we write format.html or if it is format.js, it usually tries to render what's written in views/file1/create.js or view/file1/create.html.erb. Now if suppose on creation we want to redirect to home page, then we can mention the file name like **format.html {render 'index'}** – Abhishek Jadav Aug 21 '22 at 15:26
  • You are using rails ujs? Can you show us the request in rails logs? – Pedro Augusto Ramalho Duarte Aug 21 '22 at 23:05
  • @AbhishekJadav Inside the there is a api that redirects to another page, but when I add the format.html it does not redirect to what is inside the format.js file. How do I let it redirect to the location in the format.js – Ese10 Aug 22 '22 at 02:55
  • if you want your api to return JSON data, then call API with **.json**. (for example **/file/file1/create.json**) and also add **Content-Type:application/json** in headers. Try if this adds any progress to our solution – Abhishek Jadav Aug 23 '22 at 06:12

0 Answers0