rails html file printing unwanted data from database - ruby-on-rails

i'm very new to rails and html and i'm not sure why this piece of code is printing an array of data that i didn't ask for at the bottom of my web page. I need help with getting rid of them from my web page.
show.html.erb
<table>
<tr>
<th>Name</th>
</tr>
<%= #category.products.each do |product| %>
<tr>
<td><%= product.id %></td>
<td><%= product.size %></td>
<td><%= product.name %></td>
</tr>
<% end %>
</table>
<%= #category.products.each do |product| %>
<h3><%= product.id %></h3>
<h3><%= product.size %></h3>
<h3><%= product.name %></h3>
<% end %>
screenshot of the website here, red arrows are pointing to the unwanted data
Any help would be greatly appreciated, thanks.

Change <%= #category... to <% #category. on both of your loops.
By including the = erb is printing out the whole object on each loop (but you just want to read from the object)

It's been awhile since I've written any ERB code, but from what I remember the <%= operator prints the result to the output. So you should be able to convert those <%= to <% so that ERB knows to execute the ruby code, but not include the result in the output.
<table>
<tr>
<th>Name</th>
</tr>
<% #category.products.each do |product| %>
<tr>
<td><%= product.id %></td>
<td><%= product.size %></td>
<td><%= product.name %></td>
</tr>
<% end %>
</table>
<% #category.products.each do |product| %>
<h3><%= product.id %></h3>
<h3><%= product.size %></h3>
<h3><%= product.name %></h3>
<% end %>

Related

Rails loop and rowspan

I'm having issues with rowspan in a loop in my form using conditional statements. My first Rowspan 2 is outside the loop and works fine, but my second Rowspan 2 is inside the conditional, therefore it does not work as it should. Is there a method to resolve this?
This is what I'm trying to achieve
Form
<table>
<th>Header 1</th>
<th>Header 2</th>
<th>Description</th>
<th>Phase</th>
<tr>
<td rowspan="4">Rowspan 4</td>
<td rowspan="2">Rowspan 2</td>
<% Identity.all.each do |identity| %>
<%= form.fields_for :indicators, form.object.indicators.where(identity: identity).first_or_initialize do |ff| %>
<%= ff.hidden_field :id %>
<%= ff.hidden_field :identity_id %>
<% if ff.object.identity.number <= 1.4 %>
<td><%= ff.object.identity.description %></td>
<td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
</tr>
<tr>
<% elsif ff.object.identity.number > 1.4 %>
<td rowspan="2">Rowspan 2</td>
<td><%= ff.object.identity.description %></td>
<td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
<% end %>
</tr>
<% end %>
<% end %>
</table>
This is definitely doable, but I think it's not working for you based on how you're partitioning the data.
Specifically, it seems like you're closing trs in weird places, as well as you create one rowspan outside the loop.
A more robust solution would likely partition the data into rowspan=X blocks (maybe by using group_by, in_groups_of, chunk)
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Description</th>
<th>Phase</th>
</tr>
<tr>
<td rowspan="4">Rowspan 4</td>
<% Identity.all.each_with_index do |identity, index| %>
<% if index.positive? %>
<tr> #emit a tr in every row but the first
<% end %>
<% if index.even? %>
<td rowspan="2">Rowspan 2</td>
<% end %>
<%= form.fields_for :indicators, form.object.indicators.where(identity: identity).first_or_initialize do |ff| %>
<%= ff.hidden_field :id %>
<%= ff.hidden_field :identity_id %>
<td><%= ff.object.identity.description %></td>
<td><%= ff.collection_select :phase_id, Phase.all, :id, :name %></td>
</tr> #Close a tr in every row
<% end %>
<% end %>
</table>
I haven't tested this personally, but I think something closer to this format would work. It would also be useful to see the html output you're actually getting to debug.

param is missing or the value is empty: record

Hi I am using Ruby on Rails to show an table, this is the code in view:
<h1> Show by category</h1>
<h2> table </h2>
<table border="1" style="width:70%">
<tr>
<th>Name</th>
<th>SKU</th>
<th>Category</th>
<th><%= link_to "New",new_record_path %>
</tr>
<%= #records.each do |r| %>
<tr>
<td><%= r.name %></td>
<td><%= r.sku %></td>
<td><%= r.category %></td>
<td><%= link_to "Edit", edit_record_path(r) %></td>
</tr>
<% end %>
<%= will_paginate #records %>
</table>
and I got this
http://i.stack.imgur.com/rUN0v.png
Between the headline and the table, the records are all shown up, I don't know where it is coming from, can somebody help?
The problem is here in this line
<%= #records.each do |r| %>
which should be
<% #records.each do |r| %>
Imp note:
<% %> #Executes the statement/expression.
<%= %> #Prints the output.

Undefined method Error message in Ruby 2.1

I have zero experience with Ruby, and I'm having the following issue:
I receive a "We're sorry, but something went wrong" error message when I try to login into my admin panel (mydomain.com/administrator).
Checking the logs I found out the following:
As well, I checked the login_controller.rb:
Here you have the template:
<h1><%= t(".title")%></h1>
<!-- Pages -->
<% if #pages.length > 0 %>
<h3><%= t(".lastets_pages")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.page.title")%></th>
<th><%=t("activerecord.attributes.page.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #pages.each do |page| %>
<tr>
<td><%= page.id %></td>
<td><%= page.title %></td>
<td><%= page.slug %></td>
<td><%= page.user.username %></td>
<td><%= l(page.created_at, format: :long) %></td>
<td><%= l(page.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_page_path(page) %>
<%= link_to t("generic.delete"), administrator_page_path(page), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_pages")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_pages_empty")%> </div>
<% end %>
<!-- Notices -->
<% if #notices.length > 0 %>
<h3><%= t(".lastets_notices")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.notice.title")%></th>
<th><%=t("activerecord.attributes.notice.countries")%></th>
<th><%=t("activerecord.attributes.notice.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #notices.each do |notice| %>
<tr>
<td><%= notice.id %></td>
<td><%= notice.title %></td>
<td><%= notice.show_countries %></td>
<td><%= notice.slug %></td>
<td><%= notice.user.username %></td>
<td><%= l(notice.created_at, format: :long) %></td>
<td><%= l(notice.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_notice_path(notice) %>
<%= link_to t("generic.delete"), administrator_notice_path(notice), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_notices")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_notices_empty")%> </div>
<% end %>
<!-- Faqs -->
<% if #faqs.length > 0 %>
<h3><%= t(".lastets_faqs")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.faq.title")%></th>
<th><%=t("activerecord.attributes.faq.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #faqs.each do |faq| %>
<tr>
<td><%= faq.id %></td>
<td><%= faq.title %></td>
<td><%= faq.slug %></td>
<td><%= faq.user.username %></td>
<td><%= l(faq.created_at, format: :long) %></td>
<td><%= l(faq.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_faq_path(faq) %>
<%= link_to t("generic.delete"), administrator_faq_path(faq), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_faqs")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_faqs_empty")%> </div>
<% end %>
Notice.rb model:
Can you help me to identify the issue? I tried to clear the cache and nothing happened.
The error backtrace points to the notice.rb:7 file, which is the line inside the block:
countries << country_post.country.name
Undefined method 'name' for nil:NilClass means you are trying to call nil.name, so for a given country_post, country_post.country returns nil.
You'll have to check this part of the code to solve the bug. Another thing you can do is use the try method:
countries << country_post.country.try(:name)
this try method will return country.name if country is not nil, ornil` otherwise:
https://github.com/rails/rails/blob/cd2d3664e3b434d15b6c19e652befb386187642f/activesupport/lib/active_support/core_ext/object/try.rb#L93
https://github.com/rails/rails/blob/cd2d3664e3b434d15b6c19e652befb386187642f/activesupport/lib/active_support/core_ext/object/try.rb#L62
In your Notice.rb model use:
countries << country_post.country.try(:name)
It seems that you are trying to use the notice.show_countries method.
The log is telling you that the method name does not exist for nil, which means that your object is nil.
Please show us the content of the "show_countries" method and please check that you have an acutal object given to the line of code responsible for the method name call.
Have you tried using a debugger ?
UPDATE:
Based on the notice code, your problem is that some post, does not have a coutry, then, country_post.country is nil which result to your error.
Either you ensure that you have a default country at the creation, or you do it in two step when trying to retrieve the information
if country_post.country.blank?
countries << 'DEFAULT_COUNTRY_NAME'
else
country << country_post.country.name
The answer by #mrcasals most directly answers the question and will result in that exception no longer being raised.
However, a more "fundamental" problem here is that you are expecting some data to be persisted to the database which is not. Rails' canonical solution to the problem of ensuring the existence of attributes is via ActiveRecord validations. The general overview is a great read, and the presence validation should do the trick for this problem in particular.

Link in entire table row seems not to use turbolinks

Im referencing to Link entire table row?
I followed the instructions and now have a link in each table row:
<% #patients.each do |patient| %>
<tr onclick="location.href='<%= patient_path(patient) %>'">
<td><%= patient.name %></td>
This generates for example such a link:
<tr onclick="location.href='/patients/18'">
My problem is now that when i click on a link turbolink isnt used and it takes very long to reload the whole page! How do i have to change my code so that turbolinks is used? Thanks
Try any of these versions, turbolinks should pick up any regular link.
<% #patients.each do |patient| %>
<tr>
<td><%= link_to patient.name, patient_path(patient) %></td>
</tr>
<% end %>
<% #patients.each do |patient| %>
<%= link_to patient_path(patient) do %>
<tr>
<td><%= patient.name %></td>
</tr>
<% end %>
<% end %>
<% #patients.each do |patient| %>
<tr>
<%= link_to patient_path(patient) do %>
<td><%= patient.name %></td>
<% end %>
</tr>
<% end %>

ruby-on-rails iterating through object attributes in view template aka .erb file

I'm using ruby on rails.
wondering if this is achievable.
Original Code
<%= form_for(:page, :url=>{:action => 'create'}) do |f| %>
<table summary="Subject Form Fields" %>
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<%end%>
desired code something along the lines of creating forms
by iterating through the object attributes.
<% for attribute in #subject.attributes.keys %>
<tr>
<td><%= attribute.humanize %></td>
<td><%= #subject.attributes[attribute].to_s %></td>
</tr>
<% end %>
so I am not sure if this is possible.
I believe what you are looking for is a .each loop:
<% #subject.attributes.each do |attribute| %>
<%= attribute.humanize %>
<% end %>
That will loop through each attribute of the #subject. If you also want to loop through the keys of each attribute, you need to add another nested loop:
<% #subject.attributes.each do |attribute| %>
<% attribute.keys.each do |key| %>
<%= attribute.humanize %> or <%= key %>
<% end %>
<% end %>
Hope that helps.

Resources