【发布时间】:2016-09-17 17:19:57
【问题描述】:
我需要获取我在 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。请帮助获取它。
【问题讨论】:
标签: ruby-on-rails ruby json ruby-on-rails-4 each