Trouble with log out in my session - html

I'm on Michael Hartl tutorial chapter 8 login and logout . I've successfully login. But the log out doesn't work .I'm staying logged in after trying to log out .
The rspec when testing for the sign in button after loggout doesn't catch the error
Here are the code. for
views/layouts/header.html.erb
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path , id: "logo"%>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class= "dropdown-menu">
<li><%= link_to "Profile", user_path(current_user) %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul >
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
helpers/sessions_helper.rb
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
current_user = user
end
def signed_in?
!current_user.nil?
end
def curent_user=(user)
#current_user = user
end
def current_user
##current_user = #current_user || User.find_by_remember_token(cookies[:remember_token])
# same thing
#current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
def sign_out
current_user = nil
cookies.delete(:remember_token)
end
end
controllers/sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = "Invalid email or password combination"
render 'new'
end
end
def destroy
sign_out
redirect_to root_path
end
end

Finaly got it working with rake db:reset

Related

Unaccounted for element link in Ruby Integration test

I am running a simple integration test trying to make sure the links I have set up are working properly. The test keeps returning 1 failure saying that it "expected exactly 2 elements matching 'a[href="/"]'" (which is my root path to my home page) but the test keeps finding 3. There is only two places where I have referenced the root path via links, and it is in my _header.html.erb and for the life of my I cannot figure out where the 3rd element could be popping up from.
Here is my _header.html.erb code:
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<li><%= link_to "Log In" '#' %></li>
</ul>
</nav>
</div>
</header>
and my routes:
Rails.application.routes.draw do
get 'users/new'
root "static_pages#home"
get 'help' => "static_pages#help"
get 'about' => "static_pages#about"
get 'contact' => "static_pages#contact"
get 'signup' => "users#new"
end
Any help would be appreciated, I a newbie.
Thanks!
I'm not sure if it's the only error, but this line:
<li><%= link_to "Log In" '#' %></li>
should have a comma between "Log In" and "#", like this:
<li><%= link_to "Log In", '#' %></li>

Rails app cannot navigate to any other page from sign in page

So I have a rails+backbone.js app that when the user clicks the link to sign in, they can't navigate to any other page by clicking on the links in the header. the header has links back to the homepage which when you hover over the link on the sign in page the bottom left of the browser says the link is pointing to "session/new" when the link should be pointing to the root url ("/#"). Any ideas as to why this happens? My code is as follows:
//application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :current_user, :logged_in?
private
def current_user
return nil unless session[:token]
#current_user ||= User.find_by_session_token(session[:token])
end
def sign_in(user)
session[:token] = user.reset_session_token!
end
def sign_out
session[:token] = nil
end
def logged_in?
return true unless session[:token].nil?
false
end
def ensure_sign_in
redirect_to new_session_url unless logged_in?
end
end
class SessionsController < ApplicationController
def new
end
def create
#user = User.find_by_credentials(params[:user][:username], params[:user][:password])
if #user
sign_in(#user)
redirect_to root_url
else
flash.now[:errors] = "Invalid username or password"
render :new
end
end
def destroy
sign_out
redirect_to root_url
end
end
//_header.html.erb
<a class="navbar-brand" href= "#">Title
<!-- <img alt="Brand" src="http://createfunnylogo.com/logo/harrypotter/Expecto.jpg"
width="200" height="50"> -->
</a>
<form class="navbar-form navbar-left" role="search">
<div class="form-group" id="bloodhound">
<div id="the-basics">
<input type="text" class="typeahead" id="nav-search" placeholder="Search">
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav">
<li><%= link_to 'Home', "#" %></li>
<% if logged_in? %>
<li><%= link_to 'Sign Out', session_url, method: :delete %></li>
<li><%= link_to 'My Profile', "#/users/#{current_user.id}" %></li>
<% else %>
<li><%= link_to 'Sign In', new_session_url%></li>
<% end %>
</ul>
</nav>
Backbone start code:
window.App= {
Models: {},
Collections: {},
Views: {},
Routers: {},
initialize: function() {
App.Collections.businesses.fetch({
success: function () {
new App.Routers.Businesses({
$rootEl: $("#content")
});
if (!Backbone.History.started) {
Backbone.history.start();
}
}
});
}
};

Undefined method :name when showing listing (ruby on rails)

I created a user profile page that shows all the listings the user made. Each listing has a 'show' link that links to a new page that shows the listing individually on a different page. I got this to work for the user profile page.
I'm using this link
<li><%= link_to "Show", user_listing_path(name: #user.name, id: listing.id) %></li>
However, now I want to create a listings index page that shows all listings of every user. Each listing again should have a 'show' link that links to a page that shows the listing individually. The same link that worked on the user profile page does not work on the listing index page.
I get the following error undefined method `name' for nil:NilClass
and it points to
<%= link_to "Show", user_listing_path(name: #user.name, id: listing.id) %>
Does anyone know why?
User show file (show.html.erb)
<div class= "showuser">
<div class="error-message">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
</div>
<h4>
<%= gravatar_for #user %>
<%= #user.name %>
</h4>
<div class="span 8">
<% if #user.listings.any? %>
<h3> Job Posts (<%= #user.listings.count %>)</h3>
<ol class="listings">
<%= render #listings %>
<% #listings.each do |listing| %>
<% end %>
</ol>
<%= will_paginate #listings %>
<% end %>
</div>
</div>
listing file (_listing.html.erb)
<li>
<h4><%= listing.title %></h4>
<p><%= listing.location %></h4><br>
<span class="content"><%= listing.description %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(listing.created_at) %> ago
</span>
<li><%= link_to "Show", user_listing_path(name: #user.name, id: listing.id) %></li>
<% if current_user?(listing.user) %>
<li><%= link_to "Edit", edit_listing_path %></li>
<%= link_to "delete", listing, method: :delete,
data: { confirm: "You sure?" },
title: listing.description %>
<% end %>
</li>
listing controller
class ListingsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy, :edit, :update]
before_action :correct_user, only: [:destroy, :edit, :update]
def create
#listing = current_user.listings.build(listing_params)
if #listing.save
flash[:success] = "Job Post created"
redirect_to current_user
else
render 'listings/new'
end
end
def edit
end
def update
if #listing.update_attributes(listing_params)
flash[:success] = "Listing updated"
redirect_to #listing
else
render 'edit'
end
end
def show
#user = User.find_by_name(params[:name])
#listing = Listing.find_by_id(params[:id])
end
def new
#listing = Listing.new
#listings = Listing.paginate(page: params[:page])
end
def destroy
#listing.destroy
redirect_to current_user
end
def index
#listings = Listing.all
#listings = Listing.paginate(page: params[:page])
#user = User.find_by_name(params[:name])
#listing = Listing.find_by_id(params[:id])
end
private
def listing_params
params.require(:listing).permit(:description, :location, :title)
end
def correct_user
#listing = current_user.listings.find_by(id: params[:id])
redirect_to current_user if #listing.nil?
end
end
listing show file (show.html.erb) Shows listing indivudally
<div class="show_listing">
<div class="col-md-6">
<div class="col-md-6">
<h3><%= #listing.title %></h3>
<h3><%= #listing.location %></h3>
<p><%= #listing.description %></p><br>
<div class="center">
<%= link_to "Apply Now", '#', class: "btn btn-info", data: {no_turbolink: true} %>
</div>
</div>
</div>
</div>
<div class="show_link_position">
<% if current_user == #listing.user %>
<%= link_to 'Edit', edit_listing_path, class: "btn btn-link" %> |
<% end %>
<%= link_to 'Back', current_user, class: "btn btn-link" %>
</div>
listing index file (index.html.erb)
<div class="top">
<div class="categories-container">
<div class="boxed grid-3 category-link ">
<li><%= link_to "Find Jobs", findjobs_path, class: "category-link"%></li>
<li><%= link_to "Post Jobs", new_path, class: "category-link" %></li>
<li><%= link_to "Find Jobs", findjobs_path, class: "category-link" %></li>
<li><%= link_to "Post Jobs", new_path, class: "category-link" %></li>
<li><%= link_to "Find Jobs", findjobs_path, class: "category-link" %></li>
<li><%= link_to "Post Jobs", new_path, class: "category-link" %></li>
<li><%= link_to "Find Jobs", findjobs_path, class: "category-link" %></li>
<li><%= link_to "Post Jobs", new_path, class: "category-link" %></li>
</div>
</div>
<div class="table-container">
<div class= "grid-8 grid-moved">
<% #listings.each do |listing| %>
<h4><%= listing.title %></h4>
<h5> <%= listing.user.name %>, Posted <%= time_ago_in_words(listing.created_at) %> ago</h5>
<h5>Job Description:</h5>
<p><%= listing.description %></p>
<p>Location: <%= listing.location %></p>
<li><%= link_to "Show", user_listing_path(name: #user.name, id: listing.id) %></li>
<br><hr><br>
<% end %>
</div>
</div>
<div class="container">
<div class="pagination">
<%= will_paginate #listings %>
</div>
</div>
routes
Rails.application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :listings
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via:'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/new', to: 'listings#new', via: 'get'
match '/users/:name/:id', to: 'listings#show', via: :get, as: :user_listing
match '/findjobs', to: 'listings#index', via: 'get'
Let me know if I forgo any important information.
You have not defined #user in the controller.
In the next line, you appropriately access the listing user, as follows:
<% if current_user?(listing.user) %>
You simply need to adjust your code to look like:
<li><%= link_to "Show", user_listing_path(name: listing.user.name, id: listing.id) %></li>
Make sense?
Try:
<li><%= link_to "Show", user_listing_path(#user, listing) %></li>
And on your controller:
def show
#user = User.find(params[:id)
#listing = Listing.find(params[:id])
end

How to only show certain nav bar elements in a certain path (ruby logic)

Currently I have the following in my header layout:
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<a class="navbar-brand">Random Name<%#= image_tag('RandomName.jpg') %></a>
</div>
<div class="navbar-container-right">
<% if current_path != root_path %>
<ul class="nav nav-pills navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Contact", contact_path %></li>
<li>
<% if current_user.present? %>
<%= link_to 'Sign Out',destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to 'Sign In', new_user_session_path %>
</li>
<li><%= link_to 'Register Now!', new_user_registration_path %><% end %></li>
</ul>
<% end %>
</div>
</div><!-- /.container-fluid -->
</nav>
This gives me an error:
undefined local variable or method `current_path' for #<#<Class:0x5699580>:0x3d9f6b0>
I want to show the elements on the right on all pages except if the user is on the root_path
I also tried:
<% unless root_path %>
which at least didn't give me an error but the elements are not shown on any page at all.
What is the proper syntax in this situation?
You can use:
unless params[:controller] = 'home' && params[:action] = 'index'
Where you need to specify your controller and action associated with root path.

Link to Admin Dashboard in View

I already have Active Admin all set up and now I was trying to add a link to the dashboard in my view.
I have the current_admin_user method in my application_controller:
def current_admin_user
return nil if user_signed_in? && !current_user.admin?
current_user
end
And my view is:
<% if current_admin_user %>
<li><%= link_to "Admin", admin_path %></li>
<% end %>
However I'm getting the error:
undefined local variable or method `current_admin_user'
Anyone knows how to solve this?
If you want to use controller methods in views, you should add this line in your application_controller:
helper_method :current_admin_user
Put the function in your application_helpers.rb file in the /helpers folder instead and it should work
def current_admin_user
return nil if user_signed_in? && !current_user.admin?
current_user
end
An alternative is to use if user_signed_in? && current_user.admin? in your view
<% if user_signed_in? && current_user.admin? %>
<li><%= link_to "Admin", admins_path %></li>
<% end %>
This should work:
<% if user_signed_in? %>
<% if current_admin_user? %>
<li><%= link_to "Admin", admin_path %></li>
<% end %>
<% end %>
If it fails try:
<% if user_signed_in? %>
<% if current_user.admin? %>
<li><%= link_to "Admin", admin_path %></li>
<% end %>
<% end %>