Ok, so I have a notifications controller similar to SO's. It shows up fine when I'm in the nofications#index page but doesn't show on any other view such as my home page.
Is there any way for me to render the notifications partial while still including it's controller gobally (have it show on any & every page)?
Thanks in advance
here's my notifications controller
class NotificationsController < ApplicationController
def index
#notifications = Notification.where('user_id = ?', current_user.id)
end
end
Create a before_filter in ApplicationController that grabs whatever notifications you want the user to see and puts them into say #notifications. Then in your layout render the partial that displays them if #notifications has something in it.
class ApplicationController
before_filter :load_notifications
def load_notifications
#notifications = Notification.where('user_id = ?', current_user.id)
end
end
Then in say app/views/layouts/application.html.erb:
<% #notifications.each do |n| %>
<%= n.message %>
<% end %>
Related
I have a user profile controller called "userinfo" and it's corresponding view. The userinfo index is the root path. In the homepage(which is the userinfo index), I have a link that takes you to the user profile page. It is giving me this error when I click on the image on the view page:
My routes are:
My userinfos_controller:
class UserinfosController < ApplicationController
before_action :find_userinfo, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
#userinfors = Userinfo.where(:userinfo_id => #userinformation_user_id)
end
def show
#myvideo = Video.last
end
def new
#userinformation = current_user.userinfos.build
end
def create
#userinformation = current_user.userinfos.build(userinfo_params)
if #userinformation.save
redirect_to root_path
else
render 'new'
end
end
def edit
end
def update
end
def destroy
#userinformation.destroy
redirect_to userinfo_path
end
private
def userinfo_params
params.require(:userinfo).permit(:name, :email, :college, :gpa, :major)
end
def find_userinfo
#userinformation = Userinfo.find(params[:id])
end
end
and my view is:
<%= link_to image_tag("student.png", class: 'right'), userinfo_path(#userinfors) %>
I thought maybe I must include ':index' in the 'before_action :find_userinfo' at the top of my controller. If I do that, the homepage doesn't even load and it gives me this error:
Try below code:
controller
def index
#userinfors = Userinfo.where(:userinfo_id => #userinformation_user_id) #pass id instead of object #userinformation_user_id
end
view
<% #userinfors.each do |u| %>
<%= link_to image_tag("student.png", class: 'right'), userinfo_path(u) %>
<% end %>
Your problem is that you're trying to do perform a lookup based on something that's not an ActiveRecord (database) attribute.
Your root goes to UserinfosController which expects #userinformation_user_id but I can't tell from your code where that comes from.
You need to define your route in order that this will be expecting for an specific param, maybe the user id, and then you're able to add the value within your view, in a link_to helper:
You could modify your routes.rb to expect an id as param:
get '/user_infors/:id', to: 'userinfos#index', as: 'userinfo_path'
Then in your controller, use a find to "find" in the database the user with such id. If you'd like to use where then that would give you a relationship with all the userinfos with the id being passed as param.
If you want so, then use Userinfo.where('userinfo_id = ?', params[:id]):
def index
#userinfors = Userinfo.find(params[:id])
end
And then in your view you can access to #userinfors:
<% #userinfors.each do |user| %>
<%= link_to image_tag 'student.png', class: 'right', userinfo_path(user) %>
<% end %>
I think you could define the index to get all the userinfors and a show method to get an specific one, as you're trying to do.
I cannot seem to find the problem. In my venues show template, I want to show the venue name, and under that, I list all the venues in the database
<%= venu.name %>
<% #venus.each do |v| %>
I get the error that #venus is nil... but it is defined in my controller:
undefined method 'each' for nil:NilClass
venues_controller.rb
class VenuesController < ApplicationController
before_action :find_venue, only: [:show, :edit, :update, :destroy]
def index
#venus = Venue.all
end
def show
render :layout => nil
#venus = Venue.all
end
def new
#venu = Venue.new
end
def create
#venu = Venue.new(venue_params)
#venu.save
end
def edit
end
def update
end
def destroy
end
private
def venue_params
params.require(:venue).permit(:name, :phone, :address, :description, :type)
end
def find_venue
#venu = Venue.find(params[:id])
end
end
I have a resources :venues route in my routes.rb.
I am not sure what is causing this problem.
In your show method, you should render at the end
def show
#venus = Venue.all
render :layout => nil
end
Remove render :layout => nil from your show action.
And in your view, you need to use the instance variable #venu instead of venu
<%= #venu.name %>
I wonder why you use two instance variables for action show
#venu (via find_venue before_filter) & #venus via the action itself.
Best practice would be removing this line from action show, since show action normally used to show details for one element from a list.
#venus = Venue.all
and use #venu set by the before_filter instead.
But if you do want to keep both then re-order the lines in show action
#venus = Venue.all
render :layout => nil
Also, change the venu to #venu in the show.html.erb and if you like correct the typo in the instance variables #venu => #venue :) (Could happen to any of us)
Usually in the index method, it should show all the venus, and in the show method it would show detailed view of each venue.
Try setting something like this:
def index
#venus = Venue.all
end
def show
render :layout => nil
#venue = Venue.find(params[:id])
end
now in show.html.erb you should be able to use
#venue.name
and in your index.html.erb, you can iterate over the venus like so:
<% #venus.each do |v| %>
<%= link_to v do %>
<%= v.name %>
<% end %>
<% end %>
The above answer is correct. You can use #venus = Venue.all in your show view but because you render first it throws you an error. Just render at the end.
I'm creating an app that allows users to access different courses on video (each course has its own 4 or 5 videos, and each video has its own page).
I created a courses_controller which allows me to index and show the courses stored in the database.
Courses_controller.rb :
class CoursesController < ApplicationController
before_action :set_course, only: [:show]
skip_before_filter :verify_authenticity_token
before_filter :authorize , except: [:index, :show]
def index
#courses = Course.all
end
def show
end
private
# Use callbacks to share common setup or constraints between actions.
def set_course
#course = Course.find(params[:id])
end
def authorize
unless User.find_by_id(session[:user_id]) and User.find_by_id(session[:user_id]).active == true
redirect_to subscriptions_path, :notice => "Vous devez souscrire à un abonnement pour avoir accès a cette page"
end
end
Each course is stored in a seeds.rb file.
courses/index.html.erb lists the courses, courses/show.html.erb shows the presentation of a specific course. These 2 parts are OK.
How to create a link to the current course from the show.html.erb page?
I mean, if I have "course1" and "course2", the link will redirect "( show.html.erb )course1 presentation" to "( ?.html.erb )course1 first video" and "( show.html.erb )course2 presentation" to "( ?.html.erb )course2 first video", etc.
Any idea?
On your index you are doing right.
def index
#courses = Course.all
end
Now in index view you should have link to show page related to each course.
<% #courses.each do |course| %>
<tr>
....
....
<td><%= link_to( 'show', course_path(course) ) %></td>
</tr>
<% end %>
To show all videos related to a course in show page start from first video, You should use kaminari or will paginate. So your show action should look like.
def show
#course = Course.find(params[:id]) #if you want to show any detail of course
#videos = #course.videos.paginate(:page => params[:page], :per_page => 1)
end
Now you will get first video of course in first show request and on each pagination click. you will see next one.
<%= #course.name %>
<%= #videos.first.url %> ##because paginate return array always
## to paginate videos
<%= will_paginate #videos %>
Trying to display some affiliate products while keeping my controllers as skinny as possible. But does anybody know why this isn't working?
undefined method `any?' for nil:NilClass
app/models/shopsense.rb
require "rest_client"
module Shopsense
def self.fetch
response = RestClient::Request.execute(
:method => :get,
:url => "http://api.shopstyle.com/api/v2/products?pid=uid7849-6112293-28&fts=women&offset=0&limit=10"
)
# !!! ATTENTION !!!
#
# It works if I put the below in `shopsense` in the controller instead
#
#products = JSON.parse(response)["products"].map do |product|
product = OpenStruct.new(product)
product
end
end
end
app/controllers/main_controller.rb
class MainController < ApplicationController
before_action :shopsense
def index
end
def shopsense
Shopsense.fetch
end
end
app/views/main/index.html.erb
<% if #products.any? %>
<% #products.each do |product| %>
<div class="product">
<%= link_to product.name %>
</div>
<% end %>
<% end %>
Your index.html.erb is requesting an instance variable #products, which isn't available through in the index action of your controller.
Put the instance variable in your index action:
def index
#products = Shopsense.fetch
end
Instance variables don't belong in a model. So you can not use #products there. Put it back into the controller and you are fine.
Correct - instance variables in rails declared in the controller are available in the view. In your case, you are declaring the instance variable inside a module, and not the controller.
Try this:
def index
#products = shopsense
end
In this case, your controller will pass on the #products instance variable to the view
Because #products should be a member of MainController to be visible inside the view.
This should work:
class MainController < ApplicationController
before_action :shopsense
...
def shopsense
#products = Shopsense.fetch
end
end
Another option is to include the Shopsense module into MainController:
module Shopsense
def fetch
...
end
end
class MainController < ApplicationController
include Shopsense
before_action :fetch
...
end
I am trying to figure out how to routes the contents of one controller into that of another.
Currently, I have two controllers -
1. Static Pages controller - this is very simple, all it used for is to yield one page (with tabbable pages)
class StaticPagesController < ApplicationController
def home
end
end
2.Guides controller - This is where a user may (currently) add guides to the db.
class GuidesController < ApplicationController
def home
end
def show
#guide = Guide.find(params[:id])
end
def index
#guide = Guide.all
end
def new
#guide = Guide.new
end
def create
#guide = Guide.new(guide_params)
if #guide.save
redirect_to '/guides'
else
render 'new'
end
end
private
def guide_params
params.require(:guide).permit(:title, :description, :image, :date, :date_end, :extra_info)
end
end
I want to display the INDEX part of the controller (which currently works at '/guides', in my root directory, or 'home' in the static pages controller.
I've tried fiddling with all of this, and my last port of call is the routes.rb file. However I am not all together sure, this seems straight forward enough and its cheesing me off not being able to do it.
One of the possible solutions is that you use:
class StaticPagesController < ApplicationController
def home
#guide = Guide.all
end
end
and copy the code from index,html.erb of your guide to home.html.erb of static pages.
other than that you can use rendering: link
EDIT:
use partial to show index of guides:
class GuidesController < ApplicationController
def index
#guide = Guide.all
render partial: "index"
end
end
in your views of guide rename "index.html.erb" to "_index.html.erb" and
in static_pages/home.html.erb add:
<%= render :partial => "guides/index" %>