I'm currently using Rails 4 and I have 3 controllers that do the following:
respond_to do |format|
format.html
format.js
end
Two of them currently work just fine, but the third controller complains that ActionController::UnknownFormat in ArticlesController#drafts. Here is the problematic controller:
def drafts
@user = current_user
@drafts = @user.articles.paginate(page: params[:page], per_page: 5).draft_and_in_order
respond_to do |format|
format.html
format.js
end
end
where the other two controller's actions containing the format snippet look similar. They all also use the same ajax call, so it probably isn't that, and the call is just a simple default ajax call to the current page (e.g.)
$.ajax({ });
And triggering the articles#drafts action looks to be good in my rails server as it issues a correct GET response. The only difference here that I thought might be the case is articles#drafts have a before_actions before it:
before_action :logged_in_user, only: [:new, :create, :update, :destroy, :drafts]
before_action :correct_user_format, only: [:new, :drafts]
since they contain some redirect_to's:
def correct_user_format
user = User.find_by(id: params[:format])
unless current_user?(user)
redirect_to root_url
end
end
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in"
redirect_to root_url
end
end
Alas, deleting these and the before_actions run into the same error. And in fact, if I were to only do:
respond_to do |format|
format.html
end
or
respond_to do |format|
format.js
end
I still run into the unknown format issue. All the controllers are subclasses of ApplicationController, and my corresponding .js.erb is put into app/views/articles/drafts.js.erb. So I'm not quite sure where or what could be causing rails to throw that error. The first few lines of my stack trace:
actionpack (4.2.0.beta4) lib/action_controller/metal/mime_responds.rb:230:in `respond_to'
actionpack (4.2.0.beta4) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0.beta4) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0.beta4) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0.beta4) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
Try changing your parameter name used in
user = User.find_by(id: params[:format])
to another name. There should be a problem with Rails parsing request format.