Console data is being passed to view - ruby-on-rails

Apologizes for my poor English.
I have a reserve controller
class Checkout::ReserveController < ApplicationController
def index
#products = Product.all
end
end
It's just return all products, but on view:
Error
View:
<h3>Escolha a opção desejada</h3>
<%= #products.each do |pr| %>
<div class="product">
<strong><%= pr.name %></strong>
<p><%= pr.description %></p>
<span class="price"><%= pr.price %></span>
</div>
<% end %>
it´s looks like rails console
What am I doing wrong?

Rookie mistake. Don't output the each. Should be this:
<% #products.each do |pr| %>
Not this
<%= #products.each do |pr| %>

Related

One to many relationship fom_for take the last 3 from all the model attributes

I have one to many relationship(oferta have many sigs) and I want to show the last three sigs from all the oferta
I tried this code but it show the last 3 sigs from each oferta
In home index.erb
<% #oferta.each do |o| %>
<% if o.sigs.exists? %>
<% for item in o.sigs.order("created_at asc").last(3).each %>
<div class="col-md-4 col-sm-4">
<div class="coll">
<br>
<%= link_to item do %>
<%= image_tag item.image.url(), skip_pipeline: true ,id: "img",height: "200px"%>
<% end %>
<h4><%=link_to item.name,item %></h4>
<p id="comment"><%= item.comment %></p>
<%= link_to "read more..", item %>
<p id="price"><%= item.price %></p>
</div>
</div>
<% end %>
<% end %>
<% end %>
In the controller
def index
#oferta = Ofertum.unscoped.first(3)
end
In ofertum model
has_many :sigs
In sig model
belongs_to :ofertum
You can do it in both ways,
#last_3_sigs = Sigs.last(3)
for getting latest records first use this
#last_3_sigs = Sig.order(created_at: :desc).limit(3)

Where is the sessions hash in the Chrome Dev Tools?

I'm storing a counter in the sessions hash that gets incremented every time I call the index action in the Store controller.
I'm printing it out in the Store view, as shown in the code and screenshot.
I know I can "reset the counter" by deleting the cookie, shown in the screen shot. And I know I print the sessions hash on the server. But how do I see the sessions hash in the chrome dev tools?
store_controller.rb
class StoreController < ApplicationController
def index
#products = Product.order(:title)
session[:counter] ||= 0
session[:counter] += 1
end
end
store/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Your Pragmatic Catalog</h1>
<p><%= session[:counter] %></p>
<% p session %>
<% cache #products do %>
<% #products.each do |product| %>
<% cache product do %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
<%= button_to 'Add to Cart', line_items_path(product_id: product) %>
</div>
</div>
<% end %>
<% end %>
<% end %>
screenshot
As described in Hartl's Rails Tutorial the sessions hash is encrypted so you wouldn't be able to view it in plain text from the browser.

undefined method `total_pages' for #<Review:0x007fe460871f08>

The error output is:
undefined method `total_pages' for #<Review:0x007fe460871f08>
Movie#Show:
def show
#review = #movie.reviews.paginate(page: params[:page], per_page: 6).order('created_at DESC').build
end
I set #movie via before_filter.
My view:
<% if #movie.now_playing %>
<% if #movie.reviews.any? %>
<% #movie.reviews.each do |review| %>
<div id="each_review_container">
<span><%= link_to #movie.creator.name, user_path(#movie.creator) %> | </span>
<span id="time"><%= review.created_at.try(:strftime,'%b %d, %Y') %></span>
<p>Rating: <%= review.rating %>/10</p>
<p><%= review.content %></p>
</div>
<% end %>
<div class="digg_pagination"><%= will_paginate #review %></div>
<% else %>
<span id="review_message">No Reviews yet!</span>
<span id="add_new_review_link"><%= link_to 'Add New Review', new_movie_review_path(#movie) %></span>
<% end %>
<% else %>
<p id="review_message">You will be able to submit a review when the movie releases</p>
<% end %>
Restarted my server and I get the same error.
Been stuck on this for a while and would appreciate any assistance, thanks!
It looks like a bug, I think you want to get reviews not one review:
def show
#reviews = #movie.reviews.order('created_at DESC').paginate(page: params[:page], per_page: 6)
end
View:
<% if #movie.now_playing %>
<% if #reviews.any? %>
<% #reviews.each do |review| %>
<div id="each_review_container">
<span><%= link_to #movie.creator.name, user_path(#movie.creator) %> | </span>
<span id="time"><%= review.created_at.try(:strftime,'%b %d, %Y') %></span>
<p>Rating: <%= review.rating %>/10</p>
<p><%= review.content %></p>
</div>
<% end %>
<div class="digg_pagination"><%= will_paginate #reviews %></div>
<% else %>
<span id="review_message">No Reviews yet!</span>
<span id="add_new_review_link"><%= link_to 'Add New Review', new_movie_review_path(#movie) %></span>
<% end %>
<% else %>
<p id="review_message">You will be able to submit a review when the movie releases</p>
<% end %>
In Your view ,you are calling #movie.reviews
Why are you writing #movie.reviews in loop ?You should be using #review of action instead.It is firing query every time you call it.
#movie.reviews in your view is causing error here's guess,since it doesn't include pagination parameter and you are trying to pagination through it.

In a show action, how to list objects that are in a associated model?

What I want to do here is to list the entries of each activities in the project.
app/controllers/projects_controller.rb :
class ProjectsController < ApplicationController
def index
#projects = Project.all
end
def show
#project = Project.find(params[:id])
#activities = #project.activities
end
end
app/views/projects/show.html.erb :
<div>
<p><%= #project.description %></p>
<% #activities.each do |activity| %>
<div>
<p><%= activity.name %></p>
<% activity.entries.each do |entry| %> <= the error is at this line
<p><%= entry.name %></p>
<% end %>
</div>
<% end %>
</div>
My models are :
Project : has many activities
Activity : has many entries, belongs to project
Entry : belong to activity
The error I obtain when I run it :
"uninitialized constant Activity::Saisy" on the show file
I don't know if I can put an each loop inside an other each loop ? Maybe that's the problem, but I can't find an other way to do it...
Thanks a lot for your help.
Try renaming activity to a, maybe activity is reserved work
<div>
<p><%= #project.description %></p>
<% #activities.each do |a| %>
<div>
<p><%= a.name %></p>
<% a.entries.each do |entry| %>
<p><%= entry.name %></p>
<% end %>
</div>
<% end %>
</div>

Sort association models in Rails 3?

Menu has_many :dishes.
I want to sort the dishes by Dish.number.
Currently in my view it looks like:
<table class="menu">
<% #menu.dishes.each do |dish| %>
<div class="dish">
<tr>
<td>
<div class="dish_div dish_name">
<% if #menu.name != 'Övrigt' && #menu.name != 'Box to go' %>
<span class="dish_name"><%= "#{dish.number}. #{dish.name}" %></span>
<% else %>
<span class="dish_name"><%= "#{dish.name}" %></span>
<% end %>
<% if dish.strength_id == 2 %>
<%= image_tag('chili.png') %>
<% elsif dish.strength_id == 3 %>
<%= image_tag('chili.png') %>
<%= image_tag('chili.png') %>
<% elsif dish.strength_id == 4 %>
<%= image_tag('chili.png') %>
<%= image_tag('chili.png') %>
<%= image_tag('chili.png') %>
<% end %>
</div>
<div class="dish_div"><%= "#{dish.description}" %></div>
<div class="dish_div dish_price"><%= "#{dish.price} kr" %></div>
</td>
</tr>
</div>
<% end %>
</table>
How do I do that?
Should it be in the view or controller?
Thanks
Neither! :) --- do it in your model definitions
If you always want to order on strength:
class Menu
has_many :dishes, :order=>'strength_id DESC'
end
class Dish
belongs_to :menu
end
Otherwise, just order in the view:
<% #menu.dishes.sort_by{|dish| dish.strength_id}.each do |dish| %>
In your controller:
def list
#dishes = #menu.dishes.all(:order => :number)
end
In your view:
<% #dishes.each do |dish| %>
I'm not sure if I understood what you're trying to do, but … to iterate the dishes sorted by their number attribute, you just need to use the :order option on the dishes:
<% #menu.dishes.all(:order => :number).each do |dish| %>
...
<% end %>
You can use the order method:
<% #menu.dishes.order(number: :asc).each do |dish| %>

Resources