Rails: How to access two models in a single loop - ruby-on-rails

I am using Audited gem to track the changes done on users. What I want to display exactly is the changes made which is the audits with the time they are updated at in a same row.I tried to display it this way but it makes repetitions.
<% #users.each do |user| %>
<% user.audits.each do |audit| %>
<% if audit.action != "create" %>
<% user.revisions.each do |revision| %>
<tr class="list-item">
<td>
<%= user.role %>
</td>
<td>
<%= audit.action %>
</td>
<td>
<%= audit.audited_changes %>
</td>
<td>
<%= revision.created_at %>
</td>
<td>
<%= revision.updated_at %>
</td>
</tr>
<% end %>
<% end %>
<% end %>
<% end %>
What I want is to be able to display them with out displaying a single change multiple times.
I am looking for an expression like
for(i=5,j=3;j<5;i++,j++){
....
}
in rails, to avoid the repetition. Thank you

I solved it this way since both audits and revision has same number of elements.I hope it helps for the others.
<% #users.each do |user| %>
<% user.audits.each_with_index do |audit, i| %>
<% if audit.action != "create" %>
<% user.revisions[i] %>
<tr class="list-item">
<td>
<%= user.role %>
</td>
<td>
<%= audit.action %>
</td>
<td>
<%= audit.audited_changes %>
</td>
<td>
<%=user.revisions[i].created_at %>
</td>
<td>
<%= user.revisions[i].updated_at %>
</td>
</tr>
<% end %>
<% end %>
<% end %>

You could also try zip:
user.audits.zip(user.revisions) do |audit, revision|

Related

How to place two elements in a row while using .each loop in rails

I want to show two columns of the code in table data tag"" per row while using '.each' method. But the issue is that the following code displays one column in a row.
<table>
<% #lease.apartment.roommates.each do |roommate| %>
<tr>
<td colspan="5">
<% unless roommate == #lease.second_occupant || roommate == #lease.user %>
<% if roommate.current_room.present? %>
<p>
<%= roommate.full_name %> -
<% if roommate.current_room.apartment == #lease.apartment%>
<%= roommate.current_room&.label %>
<% end %>
<br>Email:<%= roommate.email %><br>Phone:<%= roommate.phone %><br>
<% if #lease.end_at.present? %>
Lease End date (if applicable):<%= #lease.end_at %>
<% end %>
</p>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</table>
You can use the each_slice method for dividing the array into slices of 2 elements each:
<table>
<% #roommates.each_slice(2) do |roommate_slice| %>
<tr>
<td colspan="5">
<%= roommate_slice[0].full_name %>
<%= roommate_slice[1].full_name %>
</td>
</tr>
<% end %>
</table>

How can I display country code and last 3 digits of phone number in Ruby on Rails views?

I am a beginner in RoR.
This is the original phone number +77123456999
I am going to display it as +77xxxxxx999
Here is my index.html.erb file code.
<tbody>
<% #users.each do |user| %>
<tr>
<td>
<%= user.username %>
</td>
<td>
<%= user.phone %>
</td>
<td>
<%= user.email %>
</td>
<td>
<%= user.state %>
</td>
</tr>
<% end %>
</tbody>
The value of user.phone is +77123456999
How can I change is as I want?
I would use gsub with a regexp:
phone_number = "+77123456999"
phone_number.gsub(/(?<=\w{2})\w(?=\w{3})/, 'x')
#=> "+77xxxxxx999"

rails loop take 5 and show next 5

Hello i have a loop in my rails project.
i want to display 5 events in the dashboard controller.
is there a way to show the next 5 events?
<% #events.take(5).each do |event| %>
<tr>
<td> <%= link_to event.title, event %> </td>
<td> <%= event.eventdate %> </td>
<td> <%= event.user_id %> </td>
<td> <%= event.created_at %> </td>
<td> <%= link_to "Aanpassen", edit_event_path(event) %> </td>
<td> <%= button_to "X", event , :method => :delete %> </td>
</tr>
<% end %>
so i want to create a link to te next 5 events.
Instead of using loop use will_pageinate gem
see gem will-paginate.
and railscast for will-paginate

Ruby On Rails - Acts As Taggable - Change name of param: keyword

I wonder if/how to change the name of the param :keyword when using acts as taggable?
Today my url looks like this:
http://www.foo.bar/tagged?keyword=baz
I would like to change the "keyword" to another word.
controller
def tagged
#current_keyword = params[:keyword]
#tags = FeedEntry.tag_counts_on(:keyword)
#tagged_feed_entries = FeedEntry.tagged_with(params[:keyword]).order("published_at desc").paginate :page => params[:sida]
end
View:
<table class="table table-striped">
<tbody>
<% if #tags %>
<% #tagged_feed_entries.each do |feed_entry| %>
<tr>
<td>
<% today = Date.today %>
<% if (today === feed_entry.published_at.to_date) %>
<span class="label label-success">
<% else %>
<span class="label">
<% end %>
<%= format_stamp_to_date(feed_entry.published_at) %>
kl:
<%= I18n.localize(feed_entry.published_at, :format => '%H:%M') %>
</span>
</td>
<td><%= link_to feed_entry.name, feed_entry_path(feed_entry) %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<%= will_paginate #tagged_feed_entries, :param_name => :sida %>
You should just be able to replace all instances of params[:keyword] to params[:whatever]. Your path then becomes http://www.foo.bar/tagged?whatever=baz.
If you have a search form, you'll have to make the relevant changes there, as well.

html5 in rails3 views

I have a view with some embedded ruby in it... i want to insert a cell <td></td> into it, but when i do that it gives several error messages? This is because i concatenate a text field and a submit button into the embedded ruby.
This is my code:
<table>
<% for answer in #question.answers %>
<tr>
<!-- this displays all the possible answers -->
<td>
<%= answer.text %>
</td>
<% if current_user.can_vote_on? (#question) %> <!-- if a current user has not yet voted.. -->
<td> <%= form_tag('/vote', :method => "post") do
hidden_field_tag('vote[answer_id]', answer.id) +
submit_tag("Vote")
end %> <!-- vote button.. -->
<% end %>
</td>
</tr>
<% end %>
<% if current_user.can_vote_on? (#question) %> <!-- this shows a answer text field -->
<tr>
<td>
<%= form_tag('/vote/new_answer', :method => "post") do
hidden_field_tag('answer[question_id]', #question.id) +
hidden_field_tag('answer[user_id]', current_user.id) +
text_field_tag('answer[text]') + <!-- in here i want an extra </td><td> tag -->
submit_tag('Vote')
end %>
</td>
</tr>
<% end %>
My question is: how can i exit the embedded ruby and at the same time staying in the concatenated string...? I want to add the </td> and the <td> after the text_field_tag('answer[text]')
I tried this:
<td>
<%= form_tag('/vote/new_answer', :method => "post") do %>
<%= hidden_field_tag('answer[question_id]', #question.id) %>
<%= hidden_field_tag('answer[user_id]', current_user.id) %>
<%= text_field_tag('answer[text]') %>
</td>
<td>
<%= submit_tag('Vote') %>
<% end %>
</td>
And it works!
Thijs
Simple answer: It's not possible.
I suggest you try a different approach such as using divs inside your td elements. If I were you I wouldn't concatinate the strings together.
<%= form_tag('/vote/new_answer', :method => "post") do %>
<%= hidden_field_tag(answer[question_id], #question.id %>
... so on ...
<div class="position_it_somewhere_with_this_class"><%= submit_tag("vote") %></div>
<% end %>
You don't concatenate tags!
Also you don't use divs in table rows. put the classes in your tds
...
<%= form_tag('/vote/new_answer', :method => "post") do %>
<%= hidden_field_tag('answer[question_id]', #question.id) %>
<%= hidden_field_tag('answer[user_id]', current_user.id) %>
<%= text_field_tag('answer[text]') %>
<%= submit_tag('Vote') %>
<% end %>
</td>
</tr>
..

Resources