Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails包含一个json文件_Ruby On Rails_Json - Fatal编程技术网

Ruby on rails Rails包含一个json文件

Ruby on rails Rails包含一个json文件,ruby-on-rails,json,Ruby On Rails,Json,我有一个json文件,我想在我的设备/index.html.erb中包含json数据。我将json文件保存在assets/javascript/文件夹中。我还使用scaffold生成了我的设备视图和控制器。我的json文件是。因此我想知道如何在我的rails应用程序中包含此文件 {"records":[ {"id":"1","gdname":"Amaresh","fdname":"Rahul","adname":"Satish","sdname":"Santosh","pdname":"Jit

我有一个json文件,我想在我的设备/index.html.erb中包含json数据。我将json文件保存在assets/javascript/文件夹中。我还使用scaffold生成了我的设备视图和控制器。我的json文件是。因此我想知道如何在我的rails应用程序中包含此文件

{"records":[
 {"id":"1","gdname":"Amaresh","fdname":"Rahul","adname":"Satish","sdname":"Santosh","pdname":"Jitendra"},
  {"id":"2","gdname":"Amaresh","fdname":"Rahul","adname":"Satish","sdname":"Santosh","pdname":"Jitendra"},
 {"id":"3","gdname":"Amaresh","fdname":"Rahul","adname":"Satish","sdname":"Santosh","pdname":"Jitendra"},
   {"id":"4","gdname":"Amaresh","fdname":"Rahul","adname":"Satish","sdname":"Santosh","pdname":"Jitendra"}


]}

index.html.erb


读取文件是一种选择吗

# devices_controller.rb
def index
  @data = JSON.parse(File.read(File.join('assets/javascript/folder', 'myjson.json')))
  ...
end
您将使用它作为ruby变量,您可以在视图中使用它

更新

# inside devices_controller.rb

def index
  # place the file under public folder
  @dealers = JSON.parse(File.read(File.join('public','dealers.json')
  ...
end

# inside devices/index.html.erb
...
<p><%= @dealers['records']['id'] %></p>
...
#内部设备_controller.rb
def索引
#将文件放在公用文件夹下
@deralers=JSON.parse(File.read(File.join('public','deralers.JSON'))
...
结束
#内部设备/index.html.erb
...

...
没有这样的文件或目录@rb_sysopen-assets/javascript/dealer.json您更新的代码应该在控制器操作中,而不是在您的视图中。第二个代码
@dealers=dealer。所有的
都在覆盖json数据,但我不想将我的json文件放在公用文件夹中
# inside devices_controller.rb

def index
  # place the file under public folder
  @dealers = JSON.parse(File.read(File.join('public','dealers.json')
  ...
end

# inside devices/index.html.erb
...
<p><%= @dealers['records']['id'] %></p>
...