Layout not rendering properly as expected - ruby-on-rails

In my index action I have
#posts = Post.all
This is my index.html.erb.
<div class="row">
<div class="columns large-12 small-12 medium-12">
</div>
<p id="notice"><%= notice %></p>
<h1>Listing Posts</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<%= #posts.each do |post| %>
<tr>
<td> <%= post.id %> </td>
<td> <%= post.title %> </td>
<td> <%= post.body %> </td>
<td > <%= link_to 'Show' %> </td>
<td > <%= link_to 'Edit', edit_post_path(post) %> </td>
<td> <%= link_to 'Destroy', post, method: :delete %> </td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'NEW POST', new_post_path %>
</div>
It is rendering fine except that the #posts object is printing in a form like below just above the table tag in my browser. I know it is something silly but I can't figure that out.
[#<Post id: 4, title: "new title", body: "this is new body", created_at: "2015-12-21 07:44:42", updated_at: "2015-12-21 10:31:31">, #<Post id: 7, title: "new title", body: "dfdsfd", created_at: "2015-12-21 09:40:01", updated_at: "2015-12-21 09:40:01">]

Turns out it was something really really silly! The erb template engine requires = with erb tags to display the data while the logic is written without using the =.
It was just a simple change like this:
<% #posts.each do |post| %> # without the = symbol

hai just remove the '=' in the #posts.each line.
<%#posts.each do |post| %>, thats it.
<div class="row">
<div class="columns large-12 small-12 medium-12">
</div>
<p id="notice"><%= notice %></p>
<h1>Listing Posts</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<% #posts.each do |post| %>
<tr>
<td> <%= post.id %> </td>
<td> <%= post.title %> </td>
<td> <%= post.body %> </td>
<td > <%= link_to 'Show' %> </td>
<td > <%= link_to 'Edit', edit_post_path(post) %> </td>
<td> <%= link_to 'Destroy', post, method: :delete %> </td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'NEW POST', new_post_path %>
</div>

Related

Missing Partial for render in rails

I create partial which calling _topics_list.form.html.erb and have some code
<table class="list" cellspaceing="0">
<thead class="head_list">
<tr>
<th class="column">Name</th>
<th class="column">Description</th>
</tr>
</thead>
<% #topics.each do |topic| %>
<tr class="row">
<td data-label="Name" class="column first"><%= topic.name %></td>
<td data-label="Description" class="column column_problem">
<%= link_to topic.description, topic_path(topic), class:"link_for_column_problem" %>
</td>
<td data-label="Date" class="column"><%= topic.created_at.strftime("%d %b, %Y") %></td>
</tr>
<% end %>
</table>
Also I connect this partial with my index-file by <%= render 'topics_list'%>
When I try it I have error
Missing partial topics/_topics_list, application/_topics_list with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :arb, :jbuilder]}. Searched in:
* "/home/dse/BlackListV2/app/views"
* "/home/dse/.rvm/gems/ruby-2.7.0/gems/activeadmin-2.6.1/app/views"
* "/home/dse/.rvm/gems/ruby-2.7.0/gems/kaminari-core-1.2.0/app/views"
* "/home/dse/.rvm/gems/ruby-2.7.0/gems/devise-4.7.1/app/views"
* "/home/dse/.rvm/gems/ruby-2.7.0/gems/actiontext-6.0.2.1/app/views"
* "/home/dse/.rvm/gems/ruby-2.7.0/gems/actionmailbox-6.0.2.1/app/views"
UPDATE
<p id="notice"><%= notice %></p>
<h1 class="request_headline">All Topics</h1>
<%= render 'topics_list'%>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #topics.each do |topic| %>
<tr>
<td><%= link_to 'Show', topic %></td>
<td><%= link_to 'Edit', edit_topic_path(topic) %></td>
<td><%= link_to 'Destroy', topic, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Topic', new_topic_path %>
Code where I render partial
Your partial name is _topics_list.form.html.erb. To render this, you need to call render 'topics_list.form', not render 'topics_list'.
Try changing
<p id="notice"><%= notice %></p>
<h1 class="request_headline">All Topics</h1>
<%= render 'topics_list'%>
to
<p id="notice"><%= notice %></p>
<h1 class="request_headline">All Topics</h1>
<%= render 'topics/topics_list' %>
Also, a good idea will be to always pass local variables to partials instead of using global variables, that way you have an absolute control over values in partials.
<%= render 'topics/topics_list', topics: #topics %>
and then in partial use
<table class="list" cellspaceing="0">
<thead class="head_list">
<tr>
<th class="column">Name</th>
<th class="column">Description</th>
</tr>
</thead>
<% topics.each do |topic| %>
<tr class="row">
.....
.....
</tr>
<% end %>
</table>

How can I access to nested fields attributes using nested_forms gem

I am using nested_forms gem in my project, nested_forms uses a partial view to render the line with my nested object attributes, here is my partial view code:
<tr class="fields">
<td>
<%= f.text_field :nombre, class: 'form-control craf' %>
</td>
<td>
<%
concat f.select :idtipodocumento, CrTipoDocumento.all.collect {|p| [ p.nombre, p.id ] }, {prompt: 'Seleccione el tipo de documento'}, :class=>'form-control'
%>
</td>
<td>
<%= f.text_field :documento, class: 'form-control', maxlength: '50' %>
</td>
<% if current_user.email.include? '#cifco.gob.sv' %>
<td>
<%= f.check_box :activo %>
</td>
<td>
<%= f.check_box :impresa %>
</td>
<td>
<%= f.check_box :extra %>
</td>
<% end %>
<td>
<%= f.link_to_remove raw('<i class="fa fa-minus-circle" aria-hidden="true"></i>'), :class =>'btn btn-danger hidden-print' %>
</td>
And here is the code block of the view where this partial is rendering:
<tbody>
<fieldset id="acreditados">
<%= f.fields_for :cr_acreditados, :wrapper => false %>
</fieldset>
</tbody>
So, what I want to do is evaluate if 'impresa' == false and only then render it, but if try f.impresa it says the method 'impresa' doesnt exist for f
Here is the form full code:
<%= nested_form_for(#cr_acreditacion_cliente, :validate => true) do |f| %>
<% if #cr_acreditacion_cliente.acreditaciones == nil %>
<% else %>
<table class="table table-striped table-bordered table-hover" id="AC">
<thead>
<tr>
<th>Evento</th>
<th>Cliente</th>
<th>Stand</th>
<th>Acreditaciones</th>
<th>Acreditaciones Disponibles</th>
<th>Tipo de Acreditacion</th>
</tr>
</thead>
<tbody>
<td><%= #cr_acreditacion_cliente.cr_evento.nombre %></td>
<td><%= #cr_acreditacion_cliente.cr_cliente.nombre %></td>
<td><%= #cr_acreditacion_cliente.cr_stand.codigo %></td>
<td><%= #cr_acreditacion_cliente.acreditaciones %></td>
<td>
<p hidden id="cnt"><%= #cr_acreditacion_cliente.acreditaciones %></p>
<p id="acd">
<%= #counter = #cr_acreditacion_cliente.acreditaciones - #cr_acreditacion_cliente.cr_acreditados.count %>
</p>
</td>
<td><%= #cr_acreditacion_cliente.cr_tipocre.tipo %></td>
</tbody>
</table>
<table class="table table-striped table-bordered table-hover" id="DAC">
<thead>
<tr>
<th>Nombre</th>
<th>Tipo de Documento</th>
<th>Documento</th>
<% if current_user.email.include? '#cifco.gob.sv' %>
<th>Activo</th>
<th>Impresa</th>
<th>Extra</th>
<% end %>
<th class="hidden-print">Acciones</th>
</tr>
</thead>
<tbody>
<fieldset id="acreditados">
<%= f.fields_for :cr_acreditados, :wrapper => false %>
</fieldset>
</tbody>
</table>
<div id="addBtn">
<% if #counter >= 1 %>
<p><%= f.link_to_add raw('<i class="fa fa-plus-circle" aria-hidden="true"></i>'), :cr_acreditados, id: 'btna', :class => 'btn btn-primary lta hidden-print', "data-target" => "#DAC" %></p>
<% else %>
<p class="text-center"><h3>No dispone de mas acreditaciones</h3></p>
<% end %>
</div>
<div class="actions text-center">
<%= f.submit "Guardar Credenciales", :class => 'btn btn-success hidden-print', data: { confirm: 'Favor verifique los datos antes de almacenarlos' }%>
</div>
<% end %>
<% end %>

Rails 5 - table row while no entry exists

In my rails app, content of documents are being tagged. For documents that do not have any tags, an empty row is shown in the listing (table rows) - always at the bottom of the table. I've tried to solve this for hours, not sure where to research and I'm not getting anywhere. All help welcome!
this is the view (snippet):
<div class="row" id="annotationResults">
<div class="panel panel-default" style="background-color: white; word-wrap: break-word; font-size: 0.9em;">
<table id="tags" class="table table-hover">
<thead>
<tr>
<th>Tagged content</th>
<th>as</th>
<th>in</th>
<th></th>
</tr>
</thead>
<tbody>
<% #annotation.tags.each do |tag| %>
<tr>
<td><%= tag.content %></td>
<td><%= tag.tagtype_id %></td>
<td><%#= tag.tagtype.name %></td>
<td><%= link_to '', [tag.annotation, tag], method: :delete, data: { confirm: 'Please confirm deletion!' }, :class => "glyphicon glyphicon-remove" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
It's there because you're printing an empty td.
<td><%= tag.content %></td>
Will print the td element even if the tag.content is empty. Check the example:
td{ border: 1px solid black }
<table>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
Use your RoR snippet as you mentioned in the comment:
<% #annotation.tags.each do |tag| %>
<% unless tag.content.blank? %>
<td>
<!-- logic -->
</td>
<% end %>
<% end %>
try this,
<% if tag.content.present? %>
<%= tag.content %>
<% end %>

Format Active Record Data before rendering as JSON

I have a data table that i am going to convert to Ajax and have to format the JSON values into the right formats.
In this example how would i format the tables.created_at time-stamp to MM/DD
format...
tables = Table.where(:state => "Missouri")
respond_to do |format|
format.json { render json: tables }
end
In the DOM i would do a loop through the data and do this where i needed it: Chronic.parse(s.created_at.to_date).strftime("%m/%d")
And as you can see i have alot of additional formatting to do to the data before it is returned as JSON:
<% if #load_search.nil? %>
<tr></tr>
<% else %>
<% #load_search.each do |s| %>
<tr id="row_<%= s.id %>">
<td align="center">
<%= s.id %>
</td>
<td class="count" align="center">
<%= s.results %>
</td>
<td align="center">
<%= Chronic.parse(s.created_at.to_date).strftime("%m/%d") %>
</td>
<td align="center">
<% if s.equipment.empty? %>
All Equipment
<% else %>
<%= s.equipment.pluck(:code).to_s.gsub("[","").gsub(']','').gsub('"','') %>
<% end %>
</td>
<td align="center">
<% if s.origin_id.nil? %>
<%= s.origin_states %>
<% else %>
<%= Location.find(s.origin_id).cs unless s.origin_id.nil? %>
<% end %>
</td>
<td align="center">
<%= s.origin_radius_cs %>
</td>
<td align="center">
<% if s.dest_id.nil? %>
<%= s.dest_states %>
<% else %>
<%= Location.find(s.dest_id).cs unless s.dest_id.nil? %>
<% end %>
</td>
<td align="center">
<%= s.dest_radius_cs %>
</td>
<td align="center">
<%= s.length.to_s + ' ft.' unless s.length.nil? %>
</td>
<td align="center">
<%= s.weight.to_s + ',000 lbs' unless s.weight.nil? %>
</td>
<td align="center">
<% if s.ltl %>
Partial
<% elsif s.both %>
Full
<% else %>
Both
<% end %>
</td>
<td align="center">
<%= Chronic.parse(s.pickup.to_date).strftime("%m/%d") %>
</td>
<td align="center">
<%= Chronic.parse(s.delivery.to_date).strftime("%m/%d") unless s.delivery.nil?%>
</td>
</tr>
<% end %>
<% end %>
You can override the ActiveRecord getter as
class Model
def created_at
self[:created_at].strftime("%m/%d")
end
end

Rails renders my raw data in html

I've setup a Rails project:
User->Project->Sample
When I view my Projects as table everything is fine. I render a table template:
<%= render 'layouts/projects_table' %>
but when I render my samples for a project
<%= render 'layouts/samples_table' %>
I get the table but before the table I get my raw data rendered:
[#<Sample id: 28, name: "abcd", size: 11, quantity: 11.0, created_at: "2013-04-04 09:58:50"> ... ]
ProjectsController:
def show
#project = Project.find(params[:id])
#samples = #project.samples
end
_samples_table:
<table id="samples" class="display">
<thead>
<tr>
<th>
Sample Name
</th>
<th>
Size
</th>
<th>
Quantity
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<%= #samples.each do |sample| %>
<tr>
<td>
<%= link_to sample.name, project_sample_path(#project, sample) %>
</td>
<td>
<%= sample.size %>
</td>
<td>
<%= sample.quantity %>
</td>
<td>
<% if !sample.libraries.any?%>
<%= link_to 'Del', project_sample_path(#project, sample),
:confirm => 'Are you sure?', :method => :delete %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Everything else works fine.
Any help would be appreciated!
Oliver
Remove the = from the loop definition
<%= #samples.each do |sample| %>
should be
<% #samples.each do |sample| %>
You're outputting the return value of .each.
<%= #samples.each do |sample| %>
should be
<% #samples.each do |sample| %>

Resources