Notifications ruby on rails - ruby-on-rails

Currently I have a notification system when a patients fills out a form they request a signature from a doctor. The doctor gets notified successfully. But what I am looking for is when the doctor gets the notification they can click on it (link_to) and be redirected automatically to the form.
<% if #notifications.count > 0 %>
<ul>
<% #notifications.each do |notification| %>
<li>
<span class="notification-title"><%= notification.title %></span>
<span class="notification-message"><%= notification.message %></span>
<span class="notification-time"><%= notification.created_at.strftime("%B %e at %l:%m%P") %></span>
</li>
<% end %>
</ul>
<div class="notifications-preview-footer">
<%= link_to "See All", notifications_path %>
</div>
<% else %>
<ul>
<li>No Notifications</li>
</ul>
<% end %>

I changed your title span with link_to sample, so if the title clicked then it will go to notification show page
<% if #notifications.count > 0 %>
<ul>
<% #notifications.each do |notification| %>
<li>
<%= link_to <span class="notification-title"><%= notification.title %></span>, notification_path(notification) %>
<span class="notification-message"><%= notification.message %></span>
<span class="notification-time"><%= notification.created_at.strftime("%B %e at %l:%m%P") %></span>
</li>
<% end %>
</ul>
<div class="notifications-preview-footer">
<%= link_to "See All", notifications_path %>
</div>
<% else %>
<ul>
<li>No Notifications</li>
</ul>
<% end %>

A lot of unknowns here, first you routes.rb file needs to have the routes defined to allow this say maybe
# config/routes.rb
resources: notifications
Then in you need to define the controller actions for a view maybe show/edit?
# notifications_controller.rb
class NotificationsController < ApplicationController
def show
#notification = Notification.find(params[:id])
end
end
Then you could modify you view like above to allow a link to each notification
<% #notifications.each do |notification| %>
<li>
<%= link_to <span class="notification-title"><%= notification.title %></span>, notification_path(notification) %>
<span class="notification-message"><%= notification.message %></span>
<span class="notification-time"><%= notification.created_at.strftime("%B %e at %l:%m%P") %></span>
</li>
<% end %>
This may get you what you're looking for.

Related

How to avoid same instance variables for footer

I have a footer in my rails application that shows all recent posts and categories produced by two instances variables in the welcome_controller.rb
But then if i go to another section that has a different controller responsible i get an error as the #posts and #categories instance variables are not there to loop through and list the latest posts and categories.
welcome_controller.rb (code extract)
#categories = Category.all
#posts = Post.order("created_at DESC")
_footer.html
<div class="row footer-black">
<div class="large-3 columns text-left">
<h4>Categories</h4>
<ul>
<% #categories.each do |c| %>
<% unless c.header? %>
<% if c.restriction %>
<% if check_for_free_questions_in_restricted_category(c) %>
<li> <%= link_to c.title, category_random_free_question_path(c),'data-no-turbolink' => true %> </li>
<% else %>
<li> <%= link_to c.title, new_subscription_path,'data-no-turbolink' => true %> </li>
<% end %>
<% else %>
<li> <%= link_to c.title, category_random_question_path(c),'data-no-turbolink' => true %> </li>
<% end %>
<% end %>
<% end %>
</ul>
</div>
<div class="large-5 columns text-left">
<h4>Latest Posts</h4>
<ul>
<% #posts.each do |p| %>
<li> <%= link_to truncate(p.title, length:70), blog_post_path(p) %> </li>
<% end %>
</ul>
</div>
<div class="large-4 columns text-left social-links">
<h4>Social</h4>
<a href="#">
<i class="fa fa-facebook-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-twitter-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-google-plus-square fa-2x"></i>
</a>
</div>
</div>
is there a way to show categories and posts in footer without instantiating same variables over and over again?
The short answer is 'no'. Every variable must be instantiated (initialized) before it can be used (in this case, in your view).
Now, there are some more efficient ways of instantiating these variables than writing:
#categories = Category.all
#posts = Post.order("created_at DESC")
in multiple controllers. But, that's a different question. If you're interested in that, then ask a new question or refine this one.

Kaminari not limiting collection in Spree

For some reason Kaminari is not limiting child objects on the parent show view.
Can see the pagination links but the collection does not get limited.
What am I doing wrong?
View -
<% if #handbag.microposts.any? %>
<h3>Posts (<%= #handbag.microposts.count %>)</h3>
<div class="row">
<div class="col-md-8">
<ol class="microposts">
<% #handbag.microposts.each do |micropost| %>
<li id="micropost-<%= micropost.id %>">
<span class="user"><%= micropost.user.email %></span>
<span class="content"><%= micropost.content %></span>
<%= image_tag micropost.picture.url if micropost.picture? %>
<span class="timestamp">
Added <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
</li>
<% end %>
</ol>
<%= paginate #microposts %>
Controller -
def show
#handbag = Spree::Handbag.find(params[:id])
#microposts = #handbag.microposts.page(params[:page] || 1).per(10)
end
Thanks for any help.
You're looping through #handbag.microposts, which is the entire collection, instead of #microposts, which is the paginated collection.
So just change #handbag.microposts.each to #microposts.each

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.

List of voted pins per user

I have used the acts as votable gem to like pins. Now I want a view (my favorites) with all the pins a user has liked. I get stuck in making this happen.
If have this in my pins controller file:
def my_favorites
#pins = current_user.pins
end
def like
#pin.liked_by current_user
redirect_to :back
end
def unlike
#pin.unliked_by current_user
redirect_to :back
end
And this in my favorites view:
<% if user_signed_in? %>
<h1>Mijn favoriete recepten</h1>
<p>Bekijk hier jouw <b> nummer </b> favoriete recepten die jij lekker vindt.</p>
<div id="pins" class="transitions-enabled">
<% #pins.each do |pin| %>
<div class="box panel panel-default">
<div class="panel-body">
<b>Recept: </b>
<p><%= link_to pin.description, pin_path(pin) %></p>
<b>Toegevoegd door: </b>
<p><%= link_to image_tag(pin.user.image_file_name), pin %></p>
<div class="actions">
<%= link_to like_pin_path(pin), method: :put, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
Vind ik lekker!!!!
<% end %>
<%= link_to unlike_pin_path(pin), method: :put, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-remove"></span>
Niet mijn smaak
<% end %>
</div>
</div>
</div>
<% end %>
</div>
<% else %>
<%= render 'pages/login' %>
<% end %>
Thanks in advance for your help!!
I have done something similar:
<% if user_signed_in? %>
<% #pins.each do |pin| %>
<% if current_user.voted_for? pin %>
That should be at the top of the file
I have changed the pins controller in the following and now it is working:
def my_favorites
#pins = current_user.find_liked_items
end
Thanks for your help!

undefined method `stringify_keys' for "/accounts/15":String

I am trying to display a list of accounts, i am getting the above error in my link_to method. Below is my link_to method.
<div class="list-group">
<% #accounts.each do |account| %>
<%= link_to account.name, account_path( account.accountid ),
class: "list-group-item active" do %>
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="badge">44</span>
<% end %>
<% end %>
</div>
If you're using the block form of link_to you can't have text content (the block is your text content). If you look at rails helpers. The syntax for block form looks like:
link_to(url, html_options = {}) do
# name
end
So you need to do something like:
<div class="list-group">
<% #accounts.each do |account| %>
<%= link_to account_path( account.accountid ), class: "list-group-item active" do %>
<%= account.name %>
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="badge">44</span>
<% end %>
<% end %>
</div>

Resources