ruby-on-rails - response_to format.html 参数太少

标签 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

然后我添加了一个文件 views/auteurs/create.js.erb包含此代码:
$("<%= escape_javascript(render @auteur) %>").appendTo(".bloque_ajoute_auteur"); 

我得到的错误消息是在(成功)将新作者插入数据库后立即生成的:
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'

第 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

最佳答案

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

关于ruby-on-rails - response_to format.html 参数太少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41797163/

相关文章:

ruby-on-rails - Ruby GPX 文件解析器

ruby-on-rails - Rails 3.1-使用count查找并选择as

ruby-on-rails - 验证 Webhook 的请求

html - 我怎样才能制作背景图片 scalebale

ruby-on-rails - 如何在 factory_girl 工厂中使用设计 current_user

ruby-on-rails - 使用带有子域 lvh.me 的 Rails 和 Puma?

ruby-on-rails - 如何在 text_field 中设置默认值?

ruby-on-rails - ruby rails : Running custom validation function only when other properties are present

ruby-on-rails - fog目录不能为空,aws access key不能为空,aws secret access key不能为空

ruby-on-rails - 在 Ruby 的后台作业中保护密码