response_to format.html参数太少

thi*_*ebo 2 ruby-on-rails

在添加书目表单中,我创建了一个添加作者表单,该表单使用json添加了作者。

这很好。

但是,当作者添加到数据库时,我想显示一条成功消息。

这是我的代码:

def create
    @auteur = Auteur.new(params_auteur)
    respond_to do |q|
        if @auteur.save
            format.html{ redirect_to @auteur, notice: 'Auteur added successfully.'}
            format.js{}
            format.json{
                render json: @auteur, status: :created, location: @auteur
            }
        else
            format.html{ render action: "new"}
            format.json{ render json: @auteur.errors, status: :unprocessable_entity}
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

然后,我添加了一个views/auteurs/create.js.erb包含以下代码的文件:

$("<%= escape_javascript(render @auteur) %>").appendTo(".bloque_ajoute_auteur"); 
Run Code Online (Sandbox Code Playgroud)

我的错误消息是在(成功)将新作者插入数据库后生成的:

Completed 500 Internal Server Error in 10ms (ActiveRecord: 1.6ms)
ArgumentError (too few arguments):
app/controllers/administration/auteurs_controller.rb:23:in `format'
app/controllers/administration/auteurs_controller.rb:23:in `block in create'
app/controllers/administration/auteurs_controller.rb:21:in `create'
Run Code Online (Sandbox Code Playgroud)

第21行是response_to块的开始;第23行是format.html{ redirect_to @auteur, notice: 'Auteur ajouté.'}

我以为我已经密切关注了这一点:http : //guides.rubyonrails.org/working_with_javascript_in_rails.html#server-side-concerns

Sha*_*non 6

应该是respond_to do |format|。一旦到达format没有声明的第一个实例,它将失败。


lor*_*non 5

您的代码最终会调用Kernel 的format 实例方法,而不是使用传递给 block 的参数,respond_to这才是应该发生的情况。

此方法需要更多参数,但在本例中显然没有提供这些参数。

要解决该问题,请将参数名称从 更改qformat