从 Controller Rails 4 获取视图中 Json 数组的值

Get the value of Json Array in View from Controller Rails 4

我需要在 Rails 4.

中分配的视图页面中获取 Json 数组的值

我的控制器:profile_controller.rb

class ProfileController < ApplicationController
  before_filter :authenticate_user!

  def initialize
    super
    @search_value=[]
  end

  def search_view
    @user_gender=params[:gender]
    @user_t_table=User.where.not(gender: @user_gender)
    @user_t_table.each do |user|
    sql = "SELECT users.id, users.first_name FROM users LEFT JOIN user_profiles ON users.id = user_profiles.user_id LEFT JOIN educations ON users.id = educations.user_id LEFT JOIN usercontacts ON users.id = usercontacts.user_id WHERE  user_profiles.height >='1' AND user_profiles.height <='3' AND user_profiles.marital_status='1' AND user_profiles.mother_tongue='1' AND usercontacts.country_id='1' AND usercontacts.state_id='1' AND usercontacts.city_id='1' AND educations.highest_education='1' AND users.religion_id='1' AND users.caste_id='1' AND users.id ='#{user.id}'"
    records_array = ActiveRecord::Base.connection.execute(sql)
    @search_value << records_array
  end
end

我需要在视图页面中打印@search_value,这是我的json输出,当我将渲染设置为值时得到的输出。

Json 输出为:

[[{"id":1,"first_name":"sharma","0":1,"1":"sharma"}],[]]

我的浏览页面是search_view.html.erb

<% @search_value.each_with_index do |group,index| %>
  <!-- Render stuff -->
  <% group.each_with_index do |child,indexs| %>
    <!-- Render child stuff -->
    <%= child  %>
  <% end %>
<% end %>

我需要从 json 输出中获取 id。请帮忙拿下。

尝试:Array#flatten

 > search_result = [[{"id":1,"first_name":"sharma","0":1,"1":"sharma"}],[]]
 > search_result.flatten
 #=> [{:id=>1, :first_name=>"sharma", :"0"=>1, :"1"=>"sharma"}]

在你的视图文件中search_view.html.erb:

<% @search_value.flatten.each_with_index do |group,index| %>
  <%= group["id"] %>      
  <%= group["first_name"] %>
<% end %>

注意: 你的 @search_value 的 JSON 输出看起来不像 json。 json 看起来像 "[[{\"id\":1,\"first_name\":\"sharma\",\"0\":1,\"1\":\"sharma\"}],[]]" 无论如何你可以使用 JSON.parse

解析你的 json

你可以使用 JSON.parse:

JSON.parse(group)["id"]

虽然在视图助手方法中会更好。

您可以这样更改 search_view:

def search_view
  @user_gender=params[:gender]
  @user_t_table=User.where.not(gender: @user_gender)
  @search_value += @user_t_table.flat_map do |user|
    sql = "SELECT users.id, users.first_name FROM users LEFT JOIN user_profiles ON users.id = user_profiles.user_id LEFT JOIN educations ON users.id = educations.user_id LEFT JOIN usercontacts ON users.id = usercontacts.user_id WHERE  user_profiles.height >='1' AND user_profiles.height <='3' AND user_profiles.marital_status='1' AND user_profiles.mother_tongue='1' AND usercontacts.country_id='1' AND usercontacts.state_id='1' AND usercontacts.city_id='1' AND educations.highest_education='1' AND users.religion_id='1' AND users.caste_id='1' AND users.id ='#{user.id}'"
    ActiveRecord::Base.connection.execute(sql)
  end
end

但您可能需要这种行为(清理请求之间的结果):

def search_view
  @user_gender=params[:gender]
  @user_t_table=User.where.not(gender: @user_gender)
  @search_value = @user_t_table.flat_map do |user|
    sql = "SELECT users.id, users.first_name FROM users LEFT JOIN user_profiles ON users.id = user_profiles.user_id LEFT JOIN educations ON users.id = educations.user_id LEFT JOIN usercontacts ON users.id = usercontacts.user_id WHERE  user_profiles.height >='1' AND user_profiles.height <='3' AND user_profiles.marital_status='1' AND user_profiles.mother_tongue='1' AND usercontacts.country_id='1' AND usercontacts.state_id='1' AND usercontacts.city_id='1' AND educations.highest_education='1' AND users.religion_id='1' AND users.caste_id='1' AND users.id ='#{user.id}'"
    ActiveRecord::Base.connection.execute(sql)
  end
end