如何从Rails中的这种响应中获得关键,价值?

时间:2013-12-12 10:29:35

标签: ruby-on-rails

从控制器端,它发送这样的数据,

{:room_id=>383, :title=>"111's new appointment", :description=>"new content", :organizer=>"4444 1111", :appointment_id=>"266", :is_owner=>true, :roomtype=>"1", :c
onference_time=>"12.12 10:00 ~ 12.12 23:00", :maximun_users=>"3", :current_user_list=>[#<Client firstname: "4444", lastname: "1111">], :users=>"1 / 3"}

我知道如何获取其他数据,但我的问题是这个,

:current_user_list=>[#<Client firstname: "4444", lastname: "1111">]

我怎样才能获得这里的关键和价值?

<% @appointments.each do |appointment| %>
            <%=Rails.logger.debug("**********APPOINTEMTN : : : : : : : ******"+appointment[:current_user_list].to_s)%>
            <% if appointment[:is_owner] %>
            <tr class="roomlist_myrow">

                <td><%=h appointment[:room_id] %></td>
                <td><%=h appointment[:title] %></td>
                <td><%=h get_roomtype_from_room_list().rassoc(appointment[:roomtype])[0] %></td>
                <td><%=h appointment[:users] %></td>
                <td style="color: #a9a9a9"><%=h appointment[:organizer] %></td>
                <td style="color: #3c3c3c"><%=h appointment[:conference_time] %></td>

                <td>
                    <% url = get_appointment_server(appointment[:appointment_id]) %>
                    <%= button_to_function "Enter", "enter_room('#{appointment[:room_id]}','#{url}')"%>
                    <%= link_to ("<button>Modify</button>".html_safe), make_params({ :action => :edit, :id => appointment[:appointment_id] }) %>
                    <%= link_to ("<button>Delete</button>".html_safe), make_params({ :action => :destroy, :id => appointment[:appointment_id] }), 
                                                                                                method: :delete, data: { confirm: 'Are you sure?' } %>
                </td>
              </tr>
            <%else%>
              <tr class="roomlist_row">

                <td><%=h appointment[:room_id] %></td>
                <td><%=h appointment[:title] %></td>
                <td><%=h get_roomtype_from_room_list().rassoc(appointment[:roomtype])[0] %></td>
                <td><%=h appointment[:users] %></td>
                <td style="color: #a9a9a9"><%=h appointment[:organizer] %></td>
                <td style="color: #3c3c3c"><%=h appointment[:conference_time] %></td>

                <td id="roomlist_notowner_td">
                    <% url = get_appointment_server(appointment[:appointment_id]) %>
                    <%= button_to_function "Enter", "enter_room('#{appointment[:room_id]}','#{url}')"%>

                </td>
              </tr>
            <%end%>

            <% end %>

如果我打印显示,

<%=Rails.logger.debug("**********APPOINTEMTN : : : : : : : ******"+appointment[:current_user_list].to_s)%>
**********APPOINTEMTN : : : : : : : ******#<ActiveRecord::Relation:0x4d48880>

有什么好的解决方案吗?

2 个答案:

答案 0 :(得分:0)

循环播放:appointment[:current_user_list].each do |user|然后将其打印user.firstname或其他任何内容。

答案 1 :(得分:0)

如果appointment[:current_user_list]是数组对象,那么您可以尝试以下公共方法:

  

hash.each_key {| key_value_array |块}   迭代哈希,为每个键调用一次块,将键和值作为参数传递。

或者

  

hash.each {| key,value |块}   迭代哈希,为每个键调用一次块,将键值作为双元素数组传递。

在此处找到:http://www.tutorialspoint.com/ruby/ruby_hashes.htm

相关问题