我需要在Rails 4中分配的View Page中获取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
我需要在View页面中打印@search_value,这是我将渲染放到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 。请帮忙搞定。
答案 0 :(得分:2)
> 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
答案 1 :(得分:0)
您可以使用JSON.parse:
JSON.parse(group)["id"]
虽然在视图辅助方法中会更好。
答案 2 :(得分:0)
您可以像这样更改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