rails loop take 5 and show next 5 - ruby-on-rails

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

Related

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: How to access two models in a single loop

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|

Updating multiple tables in one form in Rails 2.3.x

Seems there should be a super-quick Railsy way to do this, but excessive Googling has yielded nothing to clear things up.
OK. I need to update three tables from one form. The view part of the equation is well-documented (form_for, fields_for). But I'm unsure how to handle the form params that are sent to the update action in the parent controller. Note: The three tables all have a defined Rails association.
View:
<% form_for #account do |a| %>
<table>
<tr class="odd">
<td>
<%= a.label :plan_id %>
</td>
<td>
<%= a.text_field :plan_id %>
</td>
</tr>
<tr class="even">
<td>
<%= a.label :company %>
</td>
<td>
<%= a.text_field :company %>
</td>
</tr>
<% fields_for #user do |u| %>
<tr class="odd">
<td>
<%= u.label :first_name %>
</td>
<td>
<%= u.text_field :fname %>
</td>
</tr>
<tr class="even">
<td>
<%= u.label :last_name %>
</td>
<td>
<%= u.text_field :lname %>
</td>
</tr>
<tr class="odd">
<td>
<%= u.label :email %>
</td>
<td>
<%= u.text_field :email %>
</td>
</tr>
<tr class="even">
<td>
<%= u.label :phone %>
</td>
<td>
<%= u.text_field :phone %>
</td>
</tr>
<tr class="odd">
<td>
<%= u.label :title %>
</td>
<td>
<%= u.text_field :title %>
</td>
</tr>
<% end %>
<% fields_for #site do |s| %>
<tr class="even">
<td>
<%= s.label :domain %>
</td>
<td>
<%= s.text_field :domain_name %>
</td>
</tr>
<tr class="odd">
<td>
<%= s.label :url %>
</td>
<td>
<%= s.text_field :url %>
</td>
</tr>
<% end %>
<tr>
<td colspan="2">
<%= a.submit :submit %>
</td>
</tr>
</table>
<% end %>
Update action of accounts controller (This doesn't work):
account = params[:account]
user = account
site = account
account.save!
user.save!
site.save!
How do I handle the params that are posted to the update action? It seems straightforward to create new records in three tables from one form, but I don't know how to update those same three tables from the same form.
If accounts/users/sites are all related then your associations in your model will handle the "tables" for you. You should be thinking about your problem in terms of models (ie: your domain/business problem). Let's say that one user can have multiple accounts (otherwise, no point in the account model) and each account can own a site (web hosting management app?).
If you set up your associations in app/models/*.rb with the has_many etc relationships, the saving a form in the controller method (or in IRB) is pretty easy. The controller code you have is not right at all. You can't assign variables like that and expect Rails to do quite that much magic.
account.user = params[:user]
account.site = params[:site]
For nested forms. If you can't get that to work, use rails c:
account = Account.new
user = User.new
site = Site.new
account.user = user
account.site = site
account.save # will fail if you have constraints/validations
Get it to work in rails c and IRB and then look at your form and HTTP activity using Firebug or other browser tool. Put this in your controller method puts params and you'll see how nested forms post.

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>
..

How can I send the multiple ids to controller in this view?

I want to send multiple ids that selected with checkboxes to the controller in this code :
<% form_for :product do %>
<table border="1px">
<tr>
<th>
Select
</th>
<th>
Image
</th>
<th>
Product Name
</th>
<th>
Product Description
</th>
<th>
Product Price
</th>
<th>
Categories
</th>
<th colspan="3">
Actions
</th>
</tr>
<% #products.each do |p| %>
<tr>
<td>
<%= check_box_tag "product_ids[]", p.id, false, :id => "product_#{p.id}" %>
</td>
<td>
<%= image_tag p.photo.url(:thumb) , :alt => "#{p.name}" %>
</td>
<td>
<%= link_to "#{p.name}" , edit_product_path(p) %>
</td>
<td>
<%=h truncate(p.description.gsub(/<.*?>/,''),:length => 80) %>
</td>
<td>
<%=h p.price %>
</td>
<td>
<% for category in p.categories.find(:all) %>
<%= link_to "#{category.name}" , category_path(category.id) %>
<% end %>
</td>
<td>
<%= link_to 'Show' , product_path(p) %>
</td>
<td>
<%= link_to 'Edit', edit_product_path(p) %>
</td>
<td>
<%= link_to 'Remove', product_path(p), :confirm => "Are you really want to delete #{p.name} ?", :method => 'delete' %>
</td>
<% end %>
</tr>
</table>
<div id="products_nav">
<%= link_to "Add a new Product" , new_product_path %>
<%= link_to "Add a new Category" , new_category_path %>
<%= link_to "Category page" , categories_path %>
<%= link_to "Remove selected products" , delete_selected_products_path , :method => 'delete' %>
</div>
<% end %>
The code is in this line :
<%= link_to "Remove selected products" , delete_selected_products_path , :method => 'delete' %>
At first glance it looks OK to me, although the "delete_selected_products_path" route is not typical. Do you have a route specifically set up for this?
In any case, you should be able to send this delete request to the usual method and have the controller check the params.
This Railscast may help.

Resources