rails验证通过ajax添加的数据

时间:2017-02-05 22:07:50

标签: jquery ruby-on-rails ajax

我有一个模型参考书目和一个模型作者。

添加参考书目时,可以获得添加作者的弹出窗口。

我的问题是:如何验证并将消息发送回表单?

我使用remote形式这样做:

<%= form_for @author, url: administration_author_createjson_path, remote: true do |f| %>

这是在我的模型中:

validates :given_name, presence: true
validates :name, presence: true

这是控制器:

def createjson
    @author = Author.new(params_auteur)

    respond_to do |format|
        if @author.save
            format.html{}
            format.js{}
            format.json{
                render json: @author, status: :created, location: administration_nouveau_biblio_path
            }
        else
            format.html{ render action: "new"}
            format.json{ render json: @author.errors, status: :unprocessable_entity}
        end
    end
end

验证有效,这意味着如果缺少given_name或name,则数据不会保存到数据库中。

但在这种情况下,我不知道如何将正确的消息发送回视图。

0 个答案:

没有答案