我是rubyonrails的新手。现在正在为Shopify项目工作。我已经从我的一个表中检索了一个集合,但我无法在视图中打印任何特定的字段值。
我在控制器中的查询(它是一个辅助方法)。
def getcustomoptions product_id
customoptions = Productoption.where(product_id: product_id).first
return ActiveSupport::JSON.encode(customoptions)
end
helper_method :getcustomoptions
在我看来,当我在视图中调试输出时,
pdtoption = getcustomoptions(option.id)
debug pdtoption
是
{"id":3,"product_id":10647391438,"front_preview":"tomrain.s3.eu-central-1.amazonaws.com/productoptions/front_previews/000/000/003/original/collar-green-front.png?1496132797","left_side_preview":"tomrain.s3.eu-central-1.amazonaws.com/productoptions/left_side_previews/000/000/003/original/t-shirt-left-side.jpg?1496132798","back_preview":"tomrain.s3.eu-central-1.amazonaws.com/productoptions/back_previews/000/000/003/original/collar-green-back.png?1496132798","right_side_preview":"tomrain.s3.eu-central-1.amazonaws.com/productoptions/right_side_previews/000/000/003/original/tshirt-brightlights-side.jpg?1496132798","front_preview_file_name":"collar-green-front.png","front_preview_content_type":"image/png","front_preview_file_size":275338,"front_preview_updated_at":"2017-05-30T08:26:37.864Z","left_side_preview_file_name":"t-shirt-left-side.jpg","left_side_preview_content_type":"image/jpeg","left_side_preview_file_size":79742,"left_side_preview_updated_at":"2017-05-30T08:26:38.280Z","back_preview_file_name":"collar-green-back.png","back_preview_content_type":"image/png","back_preview_file_size":250070,"back_preview_updated_at":"2017-05-30T08:26:38.437Z","right_side_preview_file_name":"tshirt-brightlights-side.jpg","right_side_preview_content_type":"image/jpeg","right_side_preview_file_size":56997,"right_side_preview_updated_at":"2017-05-30T08:26:38.618Z"}
当我在视图中解码结果时,
pdtoption = ActiveSupport::JSON.decode(getcustomoptions(option.id))
debug pdtoption
是
{"id"=>3, "product_id"=>10647391438, "front_preview"=>"tomrain.s3.eu-central-1.amazonaws.com/productoptions/front_previews/000/000/003/original/collar-green-front.png?1496132797", "left_side_preview"=>"tomrain.s3.eu-central-1.amazonaws.com/productoptions/left_side_previews/000/000/003/original/t-shirt-left-side.jpg?1496132798", "back_preview"=>"tomrain.s3.eu-central-1.amazonaws.com/productoptions/back_previews/000/000/003/original/collar-green-back.png?1496132798", "right_side_preview"=>"tomrain.s3.eu-central-1.amazonaws.com/productoptions/right_side_previews/000/000/003/original/tshirt-brightlights-side.jpg?1496132798", "front_preview_file_name"=>"collar-green-front.png", "front_preview_content_type"=>"image/png", "front_preview_file_size"=>275338, "front_preview_updated_at"=>"2017-05-30T08:26:37.864Z", "left_side_preview_file_name"=>"t-shirt-left-side.jpg", "left_side_preview_content_type"=>"image/jpeg", "left_side_preview_file_size"=>79742, "left_side_preview_updated_at"=>"2017-05-30T08:26:38.280Z", "back_preview_file_name"=>"collar-green-back.png", "back_preview_content_type"=>"image/png", "back_preview_file_size"=>250070, "back_preview_updated_at"=>"2017-05-30T08:26:38.437Z", "right_side_preview_file_name"=>"tshirt-brightlights-side.jpg", "right_side_preview_content_type"=>"image/jpeg", "right_side_preview_file_size"=>56997, "right_side_preview_updated_at"=>"2017-05-30T08:26:38.618Z"}
但我无法打印/显示特定值(例如:front_preview)。我正在努力奋斗超过3个小时,但找不到适当的方法来处理这种情况。任何帮助/详细的解释表示赞赏。
感谢。
答案 0 :(得分:0)
如果仔细查看ActiveSupport::Json.decode
方法,它会解析您的Json-String并返回Ruby Hash。要访问哈希pdtoption
的数据,您需要使用普通的Ruby哈希访问器方法。
pdtoption["id"] #=> 3
要将数据从控制器传递到视图,您不需要每次都对数据进行编码和解码。您可以在助手方法中返回customoptions
并在视图中访问它。