Where is the sessions hash in the Chrome Dev Tools? - ruby-on-rails

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.

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)

Console data is being passed to view

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| %>

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.

rails 4 simple fragment caching doesn't work

I changed the following in config/enviornments/development to:
config.action_controller.perform_caching = true
I have the following view:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<% cache do %>
<% #products.each do |product| %>
<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>
</div>
</div>
<% end %>
<% end %>
I load the page, then remove the title and then load page again and the title disappears from the page. With caching I expect it to show the title. Is there any step I am missing?
EDIT:
IN rails guide example:
<% Order.find_recent.each do |o| %>
<%= o.buyer.name %> bought <%= o.product.name %>
<% end %>
<% cache do %>
All available products:
<% Product.all.each do |p| %>
<%= link_to p.name, product_url(p) %>
<% end %>
<% end %>
They seem to do the same/similar type of cache. I have tried some of their examples and nothing caches.

Multiple Loops on the same show page

I have a page that displays a users videos that he has posted and also on the same page a users videos that he has voted on.
here is my controller
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#videos = #user.videos.order("created_at desc").page(params[:page]).per_page(12)
votes = #user.videos_with_votes("created_at desc").page(params[:page]).per_page(12)
end
end
Here is my view for my show page
<div id="videos">
<% #videos.each do |video| %>
<div class="box">
<%= image_tag video.image.url(:threeacross) %>
<%= video.title %>
<%= video.description %>
<%= video.user_id %>
<%= link_to 'Show', video %>
<%= link_to 'Edit', edit_video_path(video) %>
</div>
<% end %>
</div>
I am confused on how to display the videos that the user has voted on in the same way that i am looping the users video posted?
Use an instance variable, e.g.
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#videos = #user.videos.order("created_at desc").page(params[:page]).per_page(12)
#voted_videos = #user.videos_with_votes("created_at desc").page(params[:page]).per_page(12)
end
end
<div id="voted_videos">
<% #voted_videos.each do |video| %>
<div class="box">
<%= image_tag video.image.url(:threeacross) %>
<%= video.title %>
<%= video.description %>
<%= video.user_id %>
<%= link_to 'Show', video %>
<%= link_to 'Edit', edit_video_path(video) %>
</div>
<% end %>
</div>
If the display of videos and voted videos is the same than you can look to make this code:
<div class="box">
<%= image_tag video.image.url(:threeacross) %>
<%= video.title %>
<%= video.description %>
<%= video.user_id %>
<%= link_to 'Show', video %>
<%= link_to 'Edit', edit_video_path(video) %>
</div>
a partial called _videos.html.erb that can be used for both #videos and #voted_videos.
Store this file (with the above code) in the same directory as the other view files
e.g.
<div id="voted_videos">
<% #videos.each do |video| %>
<% render "videos" %>
<% end %>
</div>
<div id="voted_videos">
<% #voted_videos.each do |video| %>
<% render "videos" %>
<% end %>
</div>
You need to use #votes instead of votes by itself. The # symbol shares the variable with the view layer so that you can do things with it (like loop over it).
there is a concept about instance variable and local variable in rails.
whenever you define a variable with # symbol that particular variable is an instance variable and is accessible across its views and partials.
for eg : #videos = #user.videos.order("created_at desc").page(params[:page]).per_page(12)
here #videos is an instance variable and persists its instance across its scope.
whereas local variables have its scope limited to its loop/boundry. Local variables are preferably used in loops where after the execution of the loop the variable is Garbage Collected and we can again use the same variable name for storing different value.
In your case votes is not accessible in your view as its a local variable, and hence to use this variable in your view you need to define this variable as instance variable (as #votes).

Resources