Ruby/Rails - Accessing params values

China☆狼群 提交于 2020-01-17 09:11:13

问题


A newbie question I assume but here we go: I have the following params:

{"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"}

In my code, I need to assigns the value of @tempfile to a local variable but I just cant figure out how. ;-)


回答1:


Most part of params are in params. So try

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



回答2:


suppose you assign response to a variable 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"}

Now,

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


来源:https://stackoverflow.com/questions/27420393/ruby-rails-accessing-params-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!