Rails - 从控制器向数据库提交JSON

时间:2022-10-13 09:24:13

I am working on a Rails app, and I am attempting to insert attributes from JSONs as database entries. I'm running into a problem, though, and would appreciate some guidance.

我正在研究Rails应用程序,我正在尝试将JSON中的属性作为数据库条目插入。不过,我遇到了一个问题,并希望得到一些指导。

I've been able to jam a few things together and come up with something that sort of works...

我已经能够把几件东西塞进去,然后想出一些有用的东西......

def create
    @report_group = Array.new

    @report_group.push({location:"home", comments:"Hello, database!"}, {location:"away", comments:"Goodbye, database!"})

    @report_group.each do |x|

        @new_report = Report.new(x)
        @new_report.user_id = current_user.id

        @new_report.save
    end
end

private
def report_params(params)
    params.permit(:user_id,:location,:comments)
end

This is a good first step - this commits two entries to my database, one for each of the hashes pushed into @report_group, but it is suffering from a problem - the create action does not reference the report_params whitelist.

这是一个很好的第一步 - 这将向我的数据库提交两个条目,每个条目用于推送到@report_group的每个哈希值,但它遇到问题 - 创建操作不引用report_params白名单。

I have built several Rails apps where entries are submitted one at a time via the standard Rails form helpers, but I have never done it with multiple JSONs like this before. Trying out the syntax I'd use in a typical form helper situation

我已经构建了几个Rails应用程序,其中通过标准的Rails表单帮助程序一次一个地提交条目,但我以前从未使用过多个这样的JSON。尝试我在典型的表单助手情况下使用的语法

@new_report = Report.new(report_params(x))

throws the expectable error undefined method permit' for #<Hash:0x007f966b35e270> but I am not sure what else to do here.

为#Hash:0x007f966b35e270>抛出可预期的错误未定义方法许可'但我不知道还有什么可以做的。

EDIT TO SHOW SOLUTION Big thanks to @oreoluwa for pointing me in the right direction. Here's the solution that I came up with.

编辑显示解决方案非常感谢@oreoluwa指出我正确的方向。这是我提出的解决方案。

def create
    @report_group = Array.new

    @report_group.push({location:"home", comments:"Hello, database!"}, {location:"away", comments:"Goodbye, database!"})

    @report_group.each do |x|

        hash = ActionController::Parameters.new(x)

        @new_report = Report.new(report_params(hash))
        @new_report.user_id = current_user.id

        @new_report.save
    end
end

private
def report_params(params)
    params.permit(:user_id,:location,:comments)
end

1 个解决方案

#1


1  

You're getting the error because a Hash is not the same as an ActionController::Parameters. In order to use the permit method with your Hash you may need to first convert it to ActionController::Parameters, as such:

您收到错误是因为Hash与ActionController :: Parameters不同。为了在你的Hash中使用permit方法,你可能需要先将它转换为ActionController :: Parameters,如下所示:

hash = {location:"home", comments:"Hello, database!"}
parameter = ActionController::Parameters.new(hash)
parameter.permit(:user_id,:location,:comments)

I don't know if that is what you're looking for, but I thought to point you in the right direction.

我不知道你是否正在寻找,但我想指出你正确的方向。

#1


1  

You're getting the error because a Hash is not the same as an ActionController::Parameters. In order to use the permit method with your Hash you may need to first convert it to ActionController::Parameters, as such:

您收到错误是因为Hash与ActionController :: Parameters不同。为了在你的Hash中使用permit方法,你可能需要先将它转换为ActionController :: Parameters,如下所示:

hash = {location:"home", comments:"Hello, database!"}
parameter = ActionController::Parameters.new(hash)
parameter.permit(:user_id,:location,:comments)

I don't know if that is what you're looking for, but I thought to point you in the right direction.

我不知道你是否正在寻找,但我想指出你正确的方向。