【问题标题】:Get the value of Json Array in View from Controller Rails 4从Controller Rails 4获取视图中Json数组的值
【发布时间】: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


    【解决方案1】:

    试试: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

    【讨论】:

    • 我得到了你所提到的扁平化输出,但鉴于我按照你建议的视图尝试代码时没有得到任何输出。
    • @NareshKumar.P: 你能提供group 的输出吗,使用&lt;%= group %&gt; 打印它或尝试使用group["id"] 可能是你没有得到你的钥匙作为符号
    • 我的 @search_value 是单独的 .json 格式。当我将它放在 json online 中时,它显示 json 是正确的。截至目前,当我将'@search_value'的值展平时,@search=@search_value.flatten 渲染:json=>@search 的输出是 [{"id":1,"first_name":"sharma"," 0":1,"1":"sharma"}]
    • @NareshKumar.P :为我提供@search 的价值。我试过了,它对我有用,check this Repl Demo
    • 我正在使用此代码获取此输出 '@search = @search_value.flatten render :json=>@search' 我的输出是:[{"id":1,"first_name":"夏尔马","0":1,"1":"夏尔马"}]
    【解决方案2】:

    您可以像这样更改 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
    

    【讨论】:

    • 我需要如何打印视图中的值 {"id"=>1, "first_name"=>"sharma", 0=>1, 1=>"sharma"} 。当我在 flat_map 末尾渲染“@search_value”时,我得到了这个输出。
    【解决方案3】:

    您可以使用 JSON.parse:

    JSON.parse(group)["id"]
    

    不过,在视图辅助方法中会更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 2015-08-24
      • 2016-04-28
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多