ArgumentError(参数太少):在rails 4.04上调用format.json时

jua*_*aza 34 gem json ruby-on-rails ruby-on-rails-3.2 ruby-on-rails-4

执行时

format.json{render json: {}, status: :ok}
Run Code Online (Sandbox Code Playgroud)

在Rails 4.0.4中,我收到以下错误:

ArgumentError (too few arguments):
Run Code Online (Sandbox Code Playgroud)

虽然我有另一个程序(使用Rails 3.2.13),其中完全相同的行执行没有问题.我在这里错过了什么吗?

任何宝石?

或使用rails 4更改语法?

Kir*_*rat 71

晴,你会得到错误ArgumentError (too few arguments):format,当你忘了该块内调用这部分代码respond_to的方法调用.

你的代码实际上应该是这样的

def action_name
  respond_to do |format|  ## Add this
    format.json { render json: {}, status: :ok}
    format.html 
    ## Other format
  end                    ## Add this
end
Run Code Online (Sandbox Code Playgroud)