Unaccounted for element link in Ruby Integration test - html

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>

Related

How do I give a bootstraps id: to a dropdown menu in HTML

For my Ruby on Rails project I am trying to add the same id to all the parts of the header.
Here is the effect I want on all the header elements
and here is the effect on the dropdown part of the menu
Right now the I can get the links to have the id "top" as can be seen in the code below. But how can apply this id to the Account menu?
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path, id: "top" %></li>
<li><%= link_to "Seach", search_path, id: "top" %></li>
<li><%= link_to "Courses", courses_path, id: "top" %></li>
<li><%= link_to "Subjects", subjects_path, id: "top" %></li>
<li><%= link_to "Instructors", instructors_path, id: "top" %></li>
<% if logged_in? %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user, id: "top" %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path, method: :delete, id: "top" %>
</li>
</ul>
</li>
You can see that I added the id: "top" to the links above and the "Profile" and "Logout" buttons in the dropdown menu, but I cannot figure out how to the "Account" part.
Thank you for your help!
You're assigning the same html id to multiple elements on the same page. That will cause errors. Ids need to be unique to each view. To add the id to the a tag in your Account section, it can be done with
<a href='#' class='...' id='unique-id-name'... >...</a>
The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).

How to attach an URL to an image

I tried different ways to display a picture in my rails app,and when the user clicks on that he should be redirected to the URL i specify.
I tried this method,"rails method"
<li <%= link_to new_gig_path %><%= image_tag 'v.png' %></li>
and just the image was showing,but the image wasn't clickable.
than i tried this"HTML method" <img src="link to an image"/>
And it worked as i wanted,the image appeared,and when i clicked on it,it redirected where i wanted.The problem is that it is wrapped in <a></a> tags,but i need the code to be wrapped in <li></li> tags,
note:I also tried to wrap the <a></a> tags in <li></li> tags but it destroyed my design,i do need it to be just in <li> tags
looking into my navigation bar,you see that my nav-bar is structured in <li>tags
#instead of this comment I want to put the code,i am asking help with
<li><%= link_to '+Add Box', new_gig_path, id: "addgig" %></li>
<li><%= link_to 'Edit Profile',edit_user_registration_path, id: "menu-overwritten" %></li>
<li><%= link_to "Log out", destroy_user_session_path, :method => :delete, id: "menu-overwritten"%></li>
More info:
Also i tried this method
<li><img src="a link to an image" height="30" width="159" ></li>
This time it worked as well,but i don't think it is gonna be responsive if i leave this method.This is definitely not the rails way.
Thank you.
<li>
<%= link_to new_gig_path do %>
<%= image_tag 'v.png', class: 'your-class' %>
<% end %>
</li>

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.

Using "link_to" inside other HTML tags

I'm using Bootstrap 3 and I can do most links, but have problems when the "link_to" function is contained in something else.
These work
<li><%= link_to "Contact", contact_path %></li>
<li><%= link_to "Sign Up", register_path %></li>
I don't know how to use "link_to" for these
<a class="navbar-brand" href="#">Project</a>
Where the path for project is root_path
Resources<b class="caret"></b>
Where the path for resources is resources_path
You can do this:
<%= link_to "Project", root_path , class: 'navbar-brand' %>
<%= link_to resources_path, class: 'dropdown-toggle', data: {toggle:'dropdown'} do %>
Resources <b class="caret"></b>
<% end %>
You don't have to use the link_to helper in cases it isn't actually helpful for you, but you can easily create the desired links using the helper like so:
link_to 'Project', '#', class: 'navbar-brand'
link_to '#', class: 'dropdown-toggle', data: { toggle: 'dropdown' } do
Resources <b class="caret"></b>
end

Trouble with log out in my session

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