Rails未知格式错误(HTML和JS) [英] Rails Unknown format error (both HTML and JS)

查看:54
本文介绍了Rails未知格式错误(HTML和JS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用Rails 4,并且我有3个控制器,它们可以执行以下操作:

I'm currently using Rails 4 and I have 3 controllers that do the following:

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

其中两个当前工作正常,但是第三个控制器抱怨ArticlesController#drafts中的ActionController :: UnknownFormat.这是有问题的控制器:

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

其中包含格式片段的其他两个控制器的动作看起来相似.它们都使用相同的ajax调用,所以可能不是这样,并且该调用只是对当前页面的简单默认ajax调用(例如)

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({ });

在我的Rails服务器中触发article#drafts动作看起来不错,因为它会发出正确的GET响应.我认为可能是这种情况的唯一区别是articles#drafts之前有一个before_actions:

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]

因为它们包含一些redirect_to:

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

A,删除这些和before_actions会遇到相同的错误.实际上,如果我只想做:

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

respond_to do |format|
  format.js
end

我仍然遇到未知格式的问题.所有的控制器都是ApplicationController的子类,而我对应的.js.erb被放入app/views/articles/drafts.js.erb中.所以我不太确定是什么地方或什么原因会导致Rails抛出该错误.我的堆栈跟踪的前几行:

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'

推荐答案

尝试将您在 user = User.find_by(id:params [:format])中使用的参数名称更改为另一个名称.Rails解析请求格式应该有问题.

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.

这篇关于Rails未知格式错误(HTML和JS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆