Ruby / Rails - 访问params值

时间:2014-12-11 10:19:43

标签: ruby ruby-on-rails-4

我假设一个新手问题但是我们走了:我有以下参数:

{"utf8"=>"✓",
 authenticity_token"=>".........",
 "import"=>
  {"csv"=>
    #<ActionDispatch::Http::UploadedFile:0x007fb59092a660
     @content_type="text/csv",
     @headers="Content-Disposition: form-data; name=\"import[csv]\";   filename=\"Users.csv\"\r\nContent-Type: text/csv\r\n",
     @original_filename="DemoUsers.csv",
     @tempfile=#<File:/var/folders/_p/w29hlx3x0cs6h026txv_rqhc0000gn/T/RackMultipart20141211-8204-1ha0i1u>>,
   "datatype"=>"users"},
 "commit"=>"Import",
 "action"=>"create",
 "controller"=>"imports"}

在我的代码中,我需要将@tempfile的值赋给局部变量,但我无法弄清楚如何。 ; - )

2 个答案:

答案 0 :(得分:1)

假设您将响应分配给变量res

res = {"utf8"=>"✓",
 authenticity_token"=>".........",
 "import"=>
  {"csv"=>
    #<ActionDispatch::Http::UploadedFile:0x007fb59092a660
     @content_type="text/csv",
     @headers="Content-Disposition: form-data; name=\"import[csv]\";   filename=\"Users.csv\"\r\nContent-Type: text/csv\r\n",
     @original_filename="DemoUsers.csv",
     @tempfile=#<File:/var/folders/_p/w29hlx3x0cs6h026txv_rqhc0000gn/T/RackMultipart20141211-8204-1ha0i1u>>,
   "datatype"=>"users"},
 "commit"=>"Import",
 "action"=>"create",
 "controller"=>"imports"}

现在,

res["import"]["csv"].tempfile

答案 1 :(得分:1)

参数的大部分都在params。所以试试

local_val = params["import"]["csv"].tempfile