Why is rails dumping data to the page on each - ruby-on-rails

I have a simple controller called list_controller with an index that basically does
def index
#lists = List.all
end
Then i have a view called index.html.haml that looks like:
%h1 My lists
= #lists.each do |list|
.row-fluid
%h2= list.title
%p= list.description
%hr
= link_to "New list", new_list_url
This works and renders but at the bottom of my list is what appears to be a ruby print of the lists objects, in this case a nice big ugly:
[#<List id: 1, title: "Petes todo list", description: "This is petes main list, all sorts of good stuff", created_at: "2012-03-26 21:42:57", updated_at: "2012-03-26 21:42:57">, #<List id: 2, title: "Petes work list", description: "A list for petes work stuff", created_at: "2012-03-26 22:09:50", updated_at: "2012-03-26 22:09:50">]
Why is this happening? How can I stop it?

You're outputting the result of the each, change:
= #lists.each do |list|
to:
- #lists.each do |list|
Haml documentation: = vs -

= #lists.each do |list| tells Haml to evaluate Ruby code to the right and then print out the return value. You should rewrite your view to
%h1 My lists
- #lists.each do |list|
.row-fluid
%h2= list.title
%p= list.description
%hr
= link_to "New list", new_list_url

Related

Item ordered by category

I have these in my database. Each item is in some category, and I want to display all items in the category ordered along with the category id. Something like this:
1
Expresso
Americano
2
Capuchino
Here is my seed code:
{content: "expresso", price: "29000", category_id: 1},
{content: "Americano", price: "29000", category_id: 1},
{content: "Capuccino", price: "35000", category_id: 2},
This is the controller action that performs the query:
def menu
#items = Item.order('content ASC')
end
In your view:
<%- Category.includes(:items).order('categories.id').each do |category| %>
<%= category.id %>
<%- category.items.each do |item| %>
<% = item.content %>

Rails i18n list of items and looping in view

How can I list elements in my yml and loop through them in the view and access their properties? My current code only gets the last item in the list. I want to loop through in the view the list of items and display their title and description elements.
e.g.
yml:
en:
hello: "Hello world"
front_page:
index:
description_section:
title: "MyTitle"
items:
item:
title: "first item"
description: "a random description"
item:
title: "second item"
description: "another item description"
view:
<%= t('front_page.index.description_section.items')do |item| %>
<%= item.title %>
<%= item.description %>
<%end %>
Result:
{:item=>{:title=>"second item", :description=>"another item description"}}
Desired Result:
first item
a random description
second item
another item description
Use this instead:
<% t('front_page.index.description_section.items').each do |item| %>
# ^ no equal sign here
<%= item[:title] %>
#^^^^ this is a hash
<%= item[:description] %>
<% end %>
Also, your items list is not defined properly:
t('front_page.index.description_section.items.item.title')
# => returns "second item" because the key `item` has been overwritten
Use the following syntax to define an array in YAML:
items:
- title: "first item"
description: "a random description"
- title: "second item"
description: "another item description"
To check this, you can do in your IRB console:
h = {:items=>[{:title=>"first item", :description=>"desc1"}, {:title=>"second item", :description=>"desc2"}]}
puts h.to_yaml
# => returns
---
:items:
- :title: first item
:description: desc1
- :title: second item
:description: desc2

Object with empty attributes in collection

I have collection of objects fetched from database and when I itterate over it with .each method I get single object with only empty values, like this:
- !ruby/object:List
attributes:
id: 4
title: Test
project_id: 10
created_at: 2013-12-29 20:53:04.087839000 Z
updated_at: 2013-12-29 20:53:04.087839000 Z
- !ruby/object:List
attributes:
id: 5
title: Testng
project_id: 10
created_at: 2013-12-29 20:53:59.087687000 Z
updated_at: 2013-12-29 20:53:59.087687000 Z
- !ruby/object:List
attributes:
id:
title:
project_id: 10
created_at:
updated_at:
What is ok, but... in my SQLite3 database I have only two rows... As doing work around here is not any problem for me I would like to understand why it happens. Could you explain me please?
Here is my controller and partial view in which I am using #current_lists variable
projects_controller.rb
def show
#project = Project.find(params[:id])
#attachment = Attachment.new
#list = #project.lists.new
#current_lists = #project.lists
#current_attachments = #project.attachments
#current_issues = #project.issues.includes(:category)
respond_to do |format|
format.html # show.html.erb
format.json { render json: #project }
end
end
_lists.html.haml
- #current_lists.each do |project|
.col-md-4
.panel.panel-default
.panel-heading
=project.title
.pull-right.text-success
%a.fa.fa-plus.fa-lg
%ul.list-group.task-list
%li.list-group-item.selected
%label.label-checkbox.inline
%input.task-finish{checked: "checked", type: "checkbox"}/
%span.custom-checkbox
SEO Optimisation
%span.pull-right
%a.task-del{href: "#"}
%i.fa.fa-trash-o.fa-lg.text-danger
/ /list-group
EDIT:
That is modal code that I use to render form which uses #list instance variable:
/ /Modal
#newList.modal.fade
.modal-dialog
.modal-content
.modal-header
%button.close{"aria-hidden" => "true", "data-dismiss" => "modal", type: "button"} ×
%h4 Modal with form
.modal-body
=form_for [#project, #list] do |f|
- if #list.errors.any?
#error_explanation
%h2= "#{pluralize(#list.errors.count, "error")} prohibited this list from being saved:"
%ul
- #list.errors.full_messages.each do |msg|
%li= msg
.form-group
= f.label :title
= f.text_field :title, class: 'form-control input-sm'
.form-group
= f.submit 'Save', class: '.btn.btn-success'
/ /.modal-content
/ /.modal-dialog
/ /.modal
Thank you in advance!
You're creating an empty list object in your projects controller with the code:
#list = #project.lists.new
A solution would be to only select saved objects from your #project.lists with:
#current_lists = #project.lists.to_a.select { |list| !list.new_record? }
This will remove unsaved records from your #current_lists array.
Edited to include solution

Rails: Controller dumps data in view

I have a simple controller with def index having the following code:
#companies = Company.all
respond_to do |format|
format.html
end
But in the view I get these errors appended after all the companies are being displayed:
[#<Company id: 4, title: "Testing #1(1)", created_at: "2013-02-05 19:14:04", updated_at: "2013-02-05 19:14:04">, #<Company id: 7, title: "Testing #1 1", created_at: "2013-02-05 19:34:48", updated_at: "2013-02-05 19:34:48">]
Updated with the view code:
= #companies.each do |company|
%li
.box
.c
%h2
= link_to company.title, company
= #companies.each do |company|
The equals sign in Haml means "print the result of this expression." You're seeing the array of companies on the page because you asked for it.
Use a hyphen instead.
- #companies.each do |company|

In my Rails app, JSON formatted results are appearing in a HTML view response

In a Rails 3.1 app, I have a controller returning a set of objects (children) in an index view using this code:
def index
#children = Child.all
#base_class = "children-index"
#title = "Your Children"
respond_to do |format|
format.html # children/index.html.erb
format.json { render :json => #children }
end
end
The index.html.erb view is written like so:
<h1><%= title %></h1>
<ul>
<%= #children.each do |child| %>
<li><%= link_to child.fullname, child_path(child) %></li>
<% end %>
</ul>
For some reason, the JSON response is getting thrown into the HTML response and I cannot determine the reason. None of my other index views are having this issue and their code is very close to the same.
John Jake Smith Jr
Jane Ann Doe
[#<Child id: 1, firstname: "John", middlename: "Jake", lastname: "Smith", suffix: "Jr", gender: "Male", dob_actual: "2011-01-05", dob_expected: "2011-01-01", height: 30, weight: 40, created_at: "2011-10-28 21:32:54", updated_at: "2011-10-28 21:32:54">, #<Child id: 2, firstname: "Jane", middlename: "Ann", lastname: "Doe", suffix: "", gender: "Female", dob_actual: "2011-05-05", dob_expected: "2011-05-01", height: 30, weight: 12, created_at: "2011-11-07 18:08:54", updated_at: "2011-11-07 18:08:54">]
That's not JSON, that's inspect output. You're getting that because each returns #children and you're using <%= here:
<%= #children.each do |child| %>
You want just this:
<% #children.each do |child| %>
Did you forget to do #children.to_json in your controller?

Resources