简体   繁体   中英

Rails respond_to throw ActionController::UnknownFormat

So, I'm trying to respond to an action with a js file.

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

In my view, I have 'my_schedule.js.erb' but it's not even executed, rails broke in the controller and throw me an error: ActionController::UnknownFormat, where I have the respond_to.

I tried to add

respond_to :js, :json, :html

at the beginning of my controller out of the actions but still not working. Need help to debug this and understand how respond_to really works.

format.js will only respond to an xhr request. You can't trigger this response by just navigating to the route that points to this controller and method.

You can test the js.erb execution by changing the respond_to block to

def my_schedule
  respond_to do |format|
    format.html
    format.js
  end
end

Then create a my_schedule.html.erb file in the same view folder as the js.erb with the following contents

<%= link_to 'Test', my_schedule_path, data: { remote: true } %>

Note that you may need to adjust that path, I'm just guessing on that.

Then navigate to the same path you were trying to before. You should see a link which, when clicked, will fire the js response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM