Skip to content

Instantly share code, notes, and snippets.

@mcsuth
Forked from OfTheDelmer/debug_1.md
Last active December 24, 2015 17:09
Show Gist options
  • Save mcsuth/6833356 to your computer and use it in GitHub Desktop.
Save mcsuth/6833356 to your computer and use it in GitHub Desktop.

#Debugging (Use The Duck… )

Find the errors in the following

1.)

My script is loading fine, but separate style sheets aren't working

 <!DOCTYPE html>
	
<html>

	<head>
		<script src="/script/script.js" type="text/javascript"></script>
		<link rel="stylesheet" type="text/css" href="/style/style.css">
	</head>

2.)

My scripts are loading, but they aren't working! How should I fix them? What's wrong?

<!DOCTYPE html>

<html>

<head>
	<script type="text/javascript" src="/script/script_1.js"></script>
	<script type="text/script" src="/script/script_2.js"></script>
</head>

3.)

If the CLICK ME button is clicked twice what does the console display?

html (the error isn't here)

…

<button id="someId">CLICK ME</button>
… 

JS

…
var count = 0;
function clickAction(){
	count += 1;
	console.log("hello "+ count)
}

document.getElementById("someId").onclick = clickAction()
…

When you click on the button twice nothing appears to happen. Ideally, when you click on the button the console should log that there were 2 onclicks and then log "Hello 2". I don't know if I should assume that there the function clickAction is within a 'window.onload = function() {' such that the JS will automatically initiate after the page loads.


4.)

What's the error?

function countPlusTenSquared(){
	var increment = 10;
	var count += increment;
	return count*count;
}

The error above is that the function is not grabbing an input. Also, there is no need to create a new variable to increment the input by 10. It's is wordy... I would re-write to... function countPlusTenSquared(input){ console.log(input + 10); }; countPlusTenSquared(5) => 15


5.)

Why is my form not working?

<form action="get" method="/">
	<input type="text" name="my_name">
	<input type="submit" value="submit name">
</form>

The text box and submit button will appear within and html file. However, the method


6.)

My double route isn't working! Why isn't the route working?

…
get "/double/:my_number" do
	
  input = params[:my_number]
  @double = 2*input
 
 erb :show
  
end

I don't think it is nessesary to assign params[:my_number] to a variable. If you want to double the [:my_number] number you can go to the show.erb file and <%= double %>.... actually, I have no idea...

What does get "/double/2" return?


7.)

My greet route isn't working! What should I change?

app.rb

get "/greet/:name" do
	
	@submitted_name = params[:name]
	
	erb :show
end

show.erb

<div>
	 Hello <%= @submitted_name %>!
</div>

When I go to /greet/world expecting Hello World!, but I get the following error:

 NameError at `/greet/world`
undefined local variable or method `id' for
 #<Sinatra::Application:0x007f892315d160>
 
 BACKTRACE
 ____________________________________________
 2.		<%= submitted_name %>

I stuck in @'s in the app.rb and show.erb file... The "Hello <% @submitted_name %>!" was not getting anything.


8.)

Is this RESTful?

I have a site http://somelist.com that stores peoples names in a list.

I made the following routes

GET '/name/new'			# for showing a new form

GET '/name/create'		# for submiting a 'new' name form,
						# and creating
						
GET '/name/:id/delete'	# for submitting a deleting for a name

GET '/name/:id/edit			# for showing a new edit form

GET '/name/:id/edit/post'	# for submitting an edit
							# from a form

The above is in an xyz.rb file that is in a folder that has another directory called "views" and "public". When you go to website.com/name/new there is a form that was created in an erb file in the views folder. The form in the erb file sends information back to the xyz

Please give a little explanation.


9.)

What do you think of my controller? I don't even need a view. Why shouldn't I put all this in my controller?

get "/" do
	pageTitle = "myApp!"
	"<!DOCTYPE html>
	<html>
		<head>
				<title>#{pagetitle}</title>
		</head>
		<body>
				<div>
					Hello there!
				</div>
		</body>
	</html>"
end

For good convention, you should keep the majority of your Ruby in your controller abc.rb file. Also, Sinatra, or other programs, may require specific directories to pull/yield certain files together... Sinatra reads the layout.erb file, which includes a <% yield %> tag inside... this yield "pulls" in your Ruby doc, which should hold your "get "/" do"...and redirect you to other erb files....Also, I don't think you can do <title>#{pagetitle}</title>...

10.)

Look at my view! This works, but why should I be doing things this way?

app.rb

get '/person/:name` do
	erb :show
end		

show.erb

	<div>
		<% person = params[:name] %>
	
		Hello <%= person %>
	</div>

No you should probably do something similar to: app.rb get '/person/:name` do @person = params[:name] erb :show end

`show.erb`

	<div>
		Hello <%= @person %>
	</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment