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 %>
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.
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.
Yesterday i made some experience with carrierwav, all works fine but at the image tag where normally only the image should displayed rails also shows the hole model, with created_at etc. Here you cann see it!
So now my view:
<% #patient.treatments.each do |treatment| %>
<tr>
<td><%= treatment.category.try(:typ) %></td>
<td><%= treatment.content %></td>
<td><%= treatment.day %></td>
<td><div class="arrow"></div></td>
</tr>
<tr>
<td colspan="5">
<%= link_to 'Löschen', [treatment.patient, treatment],
:confirm => 'Sind sie sicher?',
:method => :delete %>
<%= treatment.paintings.each do |paint| %>
<%= image_tag paint.name %>
<% end %>
</td>
</tr>
<% end %>
The problem has to be in <%= image_tag paint.name %>
Remove the = from <%= treatment.paintings.each do |paint| %>, that is making you also print the treatment.paintings array.
<% treatment.paintings.each do |paint| %>
<%= image_tag paint.name %>
<% end %>
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.