I am using Espinita to keep track of changes to my model and have multiple attributes that I would like to display in the view.
I haven't figured out how to iterate over each value without using an ||
in the iterator. I want to get the attribute name, the changed value and the date.
This is show.html:
<% @contract.contract_information.history_from_audits_for([:contract_title, :assigned_to, :action_requested..:deadline]).each do |changes| %>
<tr>
<%= changes[:contract_title] || changes[:assigned_to] || changes[:deadline] %>
<%= changes[:changed_at].strftime("%m/%d/%Y") %>
</tr>
<% end %>
How do I iterate over each audit without referencing the attribute name?
UPDATE
After making the recommended changes. I now receive the attributes/values in array form. Is there a way to beautify the results in the view?
Output
11/20/2019 [:contract_stage, ""] [:action_requested, ""] [:reason_for_contract_request, ""] [:existing_or_returning, ""] [:background_check_status, ""] [:priority, ""] [:deadline_date, Wed, 20 Nov 2019] [:commencement_date, Wed, 20 Nov 2019] davie200
View
<% @contract.contract_information.history_from_audits_for([:contract_stage, :action_requested, :reason_for_contract_request, :deadline_date, :commencement_date, :existing_or_returning, :background_check_status, :priority]).each do |audit| %>
<% audit.except(:changed_at).each do |attribute, value| %>
<tr>
<td><%= audit[:changed_at].strftime("%m/%d/%Y") %></td>
<td>
<% value.each do |val| %>
<%= val %>
<% end %>
</td>
<td><%= @contract.contract_information.audits.last.user.username %></td>
</tr>
<% end %>
<% end %>
If you have an opportunity to update Espinita in your project, I would recommend you to do it because they changed the format of the hash returned by history_from_audits_for
.
In the newer version, you will have something like this in the " History and Restoration " documentation:
model.history_from_audits_for([:name, :settings])
=> [{:changes=>{:name=>"Baz", :settings=>""}, :changed_at=>2015-05-03 15:33:58 -0700},
{:changes=>{:name=>"Arglebargle", :settings=>"IHOP"}, :changed_at=>2015-03-24 15:33:58 -0700},
{:changes=>{:name=>"Walrus"}, :changed_at=>2014-05-13 15:33:58 -0700}]
and your iteration will be as simple as:
<% @contract.contract_information.history_from_audits_for([:contract_title, :assigned_to, :deadline]).each do |audit| %>
<% audit[:changes].each do |attribute, value| %>
<tr>
<%= value %>
<%= audit[:changed_at].strftime("%m/%d/%Y") %>
</tr>
<% end %>
<% end %>
But, if you don't have a chance and you are using Rails > 3.0.0 consider the following approach:
<% @contract.contract_information.history_from_audits_for([:contract_title, :assigned_to, :deadline]).each do |audit| %>
<% audit.except(:changed_at).each do |attribute, value| %>
<tr>
<%= value %>
<%= audit[:changed_at].strftime("%m/%d/%Y") %>
</tr>
<% end %>
<% end %>
using Hash#except
.
Retrieve User from Audit
<%= @contract.contract_information.audits.last.user.username %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.