div_for within a table - ruby-on-rails-3

I am trying to create a table that populates based on all of the records for a specific model.
Here is the index.html.erb file
<%= div_for(#departments, :class => "test") do |department| %>
<tr>
<td class="indexc1">
<%= department.name %>
</td>
<td class="indexc2">
<%= department.location %>
</td>
<td class="indexc3">
<%= department.date_completed %>
</td>
<td class="indexc4">
<ul class="action">
<li><%= link_to 'Edit Info', edit_department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Edit Tasks', department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Destroy', department, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"tlink3" %></li>
</ul>
</td>
</tr>
<% end %>
This seems to properly create the table rows but does not put them into divs as expected.
I would like to be able to create one table row for each department and then be able to use AJAX to add or remove them.

If you want each row to be wrapped in a div, you'll need to add the div class and id to the tr tag, IE: (tr id="myid" class="class") rather than wrapping the div around the tr.
You can use the rails content_tag_for to generate this:
<% #departments.each do |department| %>
<%= content_tag_for(:tr, department, :class => "test") do %>
<td class="indexc1">
<%= department.name %>
</td>
<td class="indexc2">
<%= department.location %>
</td>
<td class="indexc3">
<%= department.date_completed %>
</td>
<td class="indexc4">
<ul class="action">
<li><%= link_to 'Edit Info', edit_department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Edit Tasks', department_path(department), :class=>"tlink3" %></li>
<li><%= link_to 'Destroy', department, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"tlink3" %></li>
</ul>
</td>
<% end %>
<% end %>

Related

How to show file upload button once in a nested form in rails 4?

I have a parent (playlist) -> child (tracks) structure in my rails app.
What I am doing in the form is showing attributes from the parent and then I have a table for the child attributes.
<%= form_for(#playlist) do |f| %>
<% if #playlist.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#playlist.errors.count, "error") %> prohibited this playlist from being saved:</h2>
<ul>
<% #playlist.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Playlist Name: " %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label " Image: " %>
<%= f.file_field :photo %>
</div>
<br>
<div class="field">
<h5> description: </h5>
<br>
<%= f.text_area :description, :size => "80x3"%>
</div>
<br>
<div>
<h5> Add some music: </h5>
</div>
<div class="field">
<%= f.fields_for :tracks, Track.new do |ff| %>
<%= ff.file_field :audio, :multiple => true %>
<% end %>
</div>
<% if !#playlist.tracks.blank? %>
<table id="tracks" class="display">
<thead>
<tr>
<th>Delete</th>
<th>Track</th>
<th>Album</th>
<th>Artist</th>
<th>Label</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :tracks do |ff| %>
<%= render "track_fields", :f => ff %>
<% end %>
</tbody>
</table>
<% end %>
<div class="actions">
<%= f.submit "Save" %>
</div>
<% end %>
I am using paperclip to upload the files. I am uploading images on playlist model and mp3s on the tracks model.
The code in question is the following:
<div class="field">
<%= f.fields_for :tracks, Track.new do |ff| %>
<%= ff.file_field :audio, :multiple => true %>
<% end %>
</div>
I feel this is a hack. Why do I have to add a new track for me to show the file_field (choose file button) once? Towards the bottom of the form, you will notice that I have to call
<%= f.fields_for :tracks do |ff| %>
again because I want to iterate through all the tracks for the playlist and show them in a table.
Not sure if this question makes sense, but how do I show the file_field once (I feel Track.new is a hack and I think its messing up in saving the model)?
EDIT:
here is a screenshot of what happens if I don't have Track.new:

Rails - how do I create a hyperlink to a page for a single record?

How do you make <%= post.name %> a link to its own page?
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
</tr>
<% end %>
similar what this code does, but without the string 'Show'
<td><%= link_to 'Show', post %></td>
what i always had in mind is like how php does it, you wrap it in
<a href="<?php phpcode() ?>">
how can i do similar result in ruby?
How about
<%= link_to post.name, post_path(post) %>
or even easier:
<%= link_to post.name, post %>

Convert from haml to erb

I try to convert the code from haml to erb but I'm getting stuck and don't know why.
Here is the original code I want to convert: https://github.com/gmarik/simple-backend-example/blob/master/app/views/backend/resource/_index.html.haml
And here is what I have right now. Can someone take a look at it and give me some hints. Thanks.
I doubt this line the most:
%tr[resource]{odd_or_even}
I think that it might be like:
<tr> <% #resource{odd_or_even} %>
RubyMine gave me an error at this line:
<%= paginate collection %>
<% content_for(:header) do %>
<h1><%= resource_class.model_name.human(count: 2) %></h1>
<ul class="tabs">
<li class="active"><%= link_to "Index", "#" %></li>
<li><%= link_to "New", new_resource_path %> </li>
</ul>
<table class='zebra-striped'>
<thead>
<tr>
<% attributes.each do |attr| %>
<th> <%= resource_class.human_attribute_name(attr) %></th>
<th> </th>
</tr>
</thead>
<tbody>
<% collection.each do |resource| %>
<tr> <% #resource{odd_or_even} %>
<% attributes.each do |attr| %>
<td> <%= resource.public_send(attr).to_s.truncate(20) %> </td>
<td class='row-actions'>
<%= link_to 'show', resource_path(resource) %>
|
<%= link_to 'edit', edit_resource_path(resource) %>
|
<%= link_to 'destroy', resource_path(resource), method: :delete, confirm: "Are you sure?" %>
</td>
<% end %>
</tbody>
</table>
<%= paginate collection %>
Looking at the documentation here : http://haml.info/docs/yardoc/file.REFERENCE.html#object_reference_
I'd say it's :
<tr id="<%= "#{resource.class.name.underscore}_#{resource.to_key}" %>" class="<%= resource.class.name.underscore %>">
This is to translate %tr[resource].
Now, the {odd_or_even} will just convert the result hash of the helper odd_or_even to map them as attributes on the tr.
If we take a look at the definition of the method here : https://github.com/gmarik/simple-backend-example/blob/master/app/helpers/backend/application_helper.rb
We see it's just a call to cycle in order to set an extra class. Therefore we end up with:
<tr id="<%= "#{resource.class.name.underscore}_#{resource.to_key}" %>" class="<%= "#{resource.class.name.underscore} #{cycle("odd", "even", name: "rows")}" %>">
Now, all of this won't fix the problem of paginate. Add the error message if you're still stuck on this.

Rails 3 View - Rounding Up

I have a Rails view to display a grid by PERIOD and GENERAL LEDGER, and I want to display a cumulative total. In the case below, however, my #cumulative_total in the "TOTAL" column is rounding up to the nearest dollar, so no cents are shown, even though they are displayed correctly in the general_ledger columns. Any ideas what I'm doing wrong?
<% #cumulative_total = 0 %>
<div id="gl_crosstab">
<table>
<tr>
<th>Period</th>
<% #general_ledgers.each do |g| %>
<th><%= g.general_ledger_number %></th>
<% end %>
<th>Total</th>
<th>% Expended</th>
</tr>
<% #expected_billings.group_by(&:period_id).each do |eb| %>
<tr>
<td><%= eb[1].first.period.pe_number %></td>
<% eb[1].each do|p| %>
<td><%= number_to_currency(p.expected_amount) %></td>
<% end %>
<td>
<% #cumulative_total = #cumulative_total + eb[1].inject(0){|sum,billing| sum+billing.expected_amount.to_i} %>
<%= number_to_currency( #cumulative_total ) %> </td>
<td><%= number_to_currency((#cumulative_total/#sla.project_total)*100) %> % </td>
</tr>
<% end %>
<tr>
<td><b>Total Budget</td>
<% #total_expected_billings.each do |teb| %>
<td><b><%= number_to_currency(teb[1].inject(0){|sum,billing| sum+billing.expected_amount.to_i}) %></td>
<% end %>
<td><b><%= number_to_currency(#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i}) %> </td>
<td><b><%= number_to_currency((#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i}/#sla.project_total)*100) %> % </b></td>
</tr>
</table>
</div>
I think that is because you are converting the billing amount to 'integers' while displaying:
#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i
If you change
billing.expected_amount.to_i
to
billing.expected_amount.to_f
it should work.

Advices to clean this show.html.erb view (Rails)?

The following view shows a single post and its comments:
views/posts/show.html.erb:
<h2>posts show</h2>
<span>Title: <%= #post.title %></span><br />
<span>Content: <%= #post.content %></span><br />
<span>User: <%= #post.user.username %></span><br />
<div class="post-<%= #post.id %>">
<h3><span class="vote-count"><%= #post.total_votes %></span> votes</h3><br />
<div class='voted-user'>
<% #post.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</div>
<%= link_to "Vote Up", vote_up_path(#votable, :votable_type => "Post"), :remote => true, :class => "vote-up" %><br />
<%= link_to "Vote Down", vote_down_path(#votable, :votable_type => "Post"), :remote => true, :class => "vote-down" %><br />
<h2>Comments</h2>
<p><%= link_to 'Order by Date', post_path(#post, :order_by => "created_at ASC") %></p>
<p><%= link_to 'Order by Votes', post_path(#post, :order_by => "total_votes DESC") %></p>
<% #comments.map do |comment| %>
<div class="comment-<%= comment.id %>">
<p>
<b>Comment:</b>
<%= comment.content %>
</p>
<p>
<b>Vote:</b>
<span class="vote-count"><%= comment.total_votes %></span>
<div class='voted-user'>
<% comment.votes.each do |vote| %>
<%= link_to vote.user.username, vote.user %>
<% end %>
</div>
</p>
<p>
<b>Commenter</b>
<%= link_to comment.user.username, comment.user %>
</p>
<p>
<b>Link</b>
<%= link_to "Show Post Comment", [#post, comment] %>
</p>
<p>
<b>Vote</b>
<%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %><br />
</p>
</div>
<% end %>
<%= will_paginate #comments %>
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% if current_user.id == #post.user_id %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<% end %>
<%= link_to 'Back', posts_path %>
I just focused on making things work so I totally forgot to make it clean.
I'm a Rails beginner and I would like some suggestions or advises to clean this view
(if you suggest to move code to another file please mention the name of the file and the directoy). Thanks in advance.
Good on you for wanting to clean it up. This is some of what I would do. I've included some examples of a few things here: Partials, Helpers, and also cleaned up the HTML a little to allow more control over the style in your stylesheets (which I left out, but you can figure out that part I'm sure). If this was my project, I would extract everything even more (for example, I would probably move the entre "edit/back" links at the bottom, as well as the "Order" links, to their own partials or helpers, since I would probably be using them in a lot of places throughout the application.) And if your comments are polymorphic (i.e., not just for posts), then definitely move that into its own view file. Also, I did this a rather quickly, so it might have some errors, sorry if you find any.
_post.html.erb
<article class="post-info">
<span class="title"><b>Title:</b> <%= post.title %></span>
<p><%= post.content %></p>
<span class="user"><b>User:</b> <%= post.user.username %></span>
</article>
<div class="post" id="post-<%=post.id%>">
<div class="vote-count"><%= post.total_votes %></span> votes
<ul class="voted-user">
<% post.votes.each do |vote| %>
<li><%= link_to vote.user.username, vote.user %></li>
<% end %>
</ul>
<ul class="voting">
<li><%= link_to "Vote Up", vote_up_path(post, :votable_type => "Post"), :remote => true, :class => "vote-up" %></li>
<li><%= link_to "Vote Down", vote_down_path(post, :votable_type => "Post"), :remote => true, :class => "vote-down" %></li>
</ul>
_comment.html.erb
<div class="comment" id="comment-<%=comment.id%>">
<article class="comment">
<b>Comment:</b> <%= comment.content %>
</article>
<div class="votes-total">
<b>Votes:</b> <span class="vote-count"><%= comment.total_votes %></span>
<ul class='voted-user'>
<% comment.votes.each do |vote| %>
<li><%= link_to vote.user.username, vote.user %></li>
<% end %>
</ul>
</div>
<div class="commenter">
<b>Commenter:</b> <%= link_to comment.user.username, comment.user %>
</div>
<div class="link">
<b>Link:</b> <%= link_to "Show Post Comment", [comment.post, comment] %>
</div>
<div class="vote">
<b>Vote:</b> <%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %>
</div>
</div> <!-- .comment -->
show.html.erb
<h2>Posts</h2>
<%= render #post %>
<h2>Comments</h2>
<ul class="order">
<li><%= link_to 'Order by Date', post_path(#post, :order_by => "created_at ASC") %></li>
<li><%= link_to 'Order by Votes', post_path(#post, :order_by => "total_votes DESC") %></li>
</ul>
<%= render #comments %>
<%= will_paginate #comments %>
<h2>Add a Comment</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<ul class="manage">
<li><%= edit_link_if_allowed(current_user, #post) %></li>
<li><%= link_to 'Back', posts_path %></li>
</ul>
Posts Helper
def edit_link_if_allowed(current_user, post)
link_to "Edit", edit_post_path(post) if post.user_id == current_user.id
end
You could use Partials