迭代参数哈希

发布于 2024-09-04 01:31:08 字数 1824 浏览 5 评论 0 原文

我在上传一些图片时遇到了非常令人沮丧的时间。它们显然是作为机架/多部分上传的,但我迭代参数哈希的方式一定是导致问题的原因。我真的需要一些帮助,这样我就可以停止拔头发了。

所以我有一个如下所示的 params 哈希:

参数:{"commit"=>"Submit", "sighting_report"=>[{"number_seen"=>"1", "picture" =>#<文件:/var/folders/IX/IXXrbzpCHkq68OuyY-yoI++++TI/-Tmp-/RackMultipart.85991.5>, "species_id"=>"2"}], "authenticity_token"= >"u0eN5MAfvGWtfEzrqBt4qfrL54VJ9SGX0jFLZCJ8iRM=", "sighting"=>{"sighting_date(2i)"=>"6", "名称"=>"", "sighting_date(3i)"=>"5", "县"=>"0"、"注释"=>""、"位置"=>""、"sighting_date(1i)"=>"2010"、"电子邮件"=>""}}

我的表单可以有多个目击报告,每个目击报告中包含多张图片。这是我的控制器代码:

def create_multiple
    @report = Report.new
    @report.name = params[:sighting]["name"]
    @report.sighting_date = Date.civil(params[:sighting][:"sighting_date(1i)"].to_i, params[:sighting][:"sighting_date(2i)"].to_i, params[:sighting][:"sighting_date(3i)"].to_i)
    @report.county_id = params[:sighting][:county]
    @report.location = params[:sighting][:location]
    @report.notes = params[:sighting][:notes]
    @report.email = params[:sighting][:email]
    @report.save!
    @report.reload
    for sr in params[:sighting_report] do
        sighting = SightingReport.new
        sighting.report_id = @report.id
        sighting.species_id = sr[:species_id]
        sighting.number_seen = sr[:number_seen]
        sighting.save
        if sr[:picture]
            sighting.reload
            for pic in sr[:picture] do
                p = SpeciesPic.new
                p.uploaded_picture = pic
                p.species_id = sighting.species_id
                p.report_id = @report.id
                p.save!
            end
        end
    end
    redirect_to :action => 'new_multiple'
end

我似乎无法访问图片数据本身,但可以继续访问某些字符串。但这不是文件名,而是一些带有转义字符的奇怪的东西。我可以做什么来访问实际数据?

I'm having an extremely frustrating time getting some images to upload. They are obviously being uploaded as rack/multipart but the way that I'm iterating over my params hash must be causing the problem. I could REALLY use some help, so I can stop pulling out my hair.

So I've got a params hash that looks like this:

Parameters: {"commit"=>"Submit", "sighting_report"=>[{"number_seen"=>"1", "picture"=>#<File:/var/folders/IX/IXXrbzpCHkq68OuyY-yoI++++TI/-Tmp-/RackMultipart.85991.5>, "species_id"=>"2"}], "authenticity_token"=>"u0eN5MAfvGWtfEzrqBt4qfrL54VJ9SGX0jFLZCJ8iRM=", "sighting"=>{"sighting_date(2i)"=>"6", "name"=>"", "sighting_date(3i)"=>"5", "county"=>"0", "notes"=>"", "location"=>"", "sighting_date(1i)"=>"2010", "email"=>""}}

My form can have multiple sighting reports with multiple pictures in each sighting report. Here's my controller code:

def create_multiple
    @report = Report.new
    @report.name = params[:sighting]["name"]
    @report.sighting_date = Date.civil(params[:sighting][:"sighting_date(1i)"].to_i, params[:sighting][:"sighting_date(2i)"].to_i, params[:sighting][:"sighting_date(3i)"].to_i)
    @report.county_id = params[:sighting][:county]
    @report.location = params[:sighting][:location]
    @report.notes = params[:sighting][:notes]
    @report.email = params[:sighting][:email]
    @report.save!
    @report.reload
    for sr in params[:sighting_report] do
        sighting = SightingReport.new
        sighting.report_id = @report.id
        sighting.species_id = sr[:species_id]
        sighting.number_seen = sr[:number_seen]
        sighting.save
        if sr[:picture]
            sighting.reload
            for pic in sr[:picture] do
                p = SpeciesPic.new
                p.uploaded_picture = pic
                p.species_id = sighting.species_id
                p.report_id = @report.id
                p.save!
            end
        end
    end
    redirect_to :action => 'new_multiple'
end

I can't seem to access the picture data itself, but keep getting access to some string. It's not the file name though, some strange thing with escaped characters. What can I do to access the actual data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱你一生 2024-09-11 01:31:08

您应该使用accepts_nested_attributes_for(如下所述:http://api.rubyonrails。 org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

这将消除控制器中的所有复杂性,并允许一些 Rails 魔法来处理嵌套对象的创建,这还将执行所有保存原子事务

You should be using accepts_nested_attributes_for (as described here: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

This will remove all of the complexity from the controller and allow some Rails magic to handle nested object creation. This will also perform all of the saves as one atomic transaction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文