Tom S
Tom S

Reputation: 31

Ruby on Rails: localhost:3000/my_pages_dont_show_up

When I 'rails server' the localhost:3000 works in my browser (chrome). Here's the term:

tom@toms-ubuntu:~/rails_projects/routesapp$ rails s
=> Booting WEBrick
=> Rails 3.2.16 application starting in development on http://localhost:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2016-01-12 10:56:27] INFO  WEBrick 1.3.1
[2016-01-12 10:56:27] INFO  ruby 1.9.3 (2013-11-22) [x86_64-linux]
[2016-01-12 10:56:27] INFO  WEBrick::HTTPServer#start: pid=4751 port=3000

But for localhost:3000/page/home the browser displays:

NoMethodError in Page#home

Showing /home/tom/rails_projects/routesapp/app/views/layouts/application.html.erb where line #5 raised:

undefined method `[]' for nil:NilClass
Extracted source (around line #5):

2: <html>
3: <head>
4:   <title>Routesapp</title>
5:   <%= stylesheet_link_tag    "application", :media => "all" %>
6:   <%= javascript_include_tag "application" %>
7:   <%= csrf_meta_tags %>
8: </head>

Rails.root: /home/tom/rails_projects/routesapp

Application Trace | Framework Trace | Full Trace app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___1001173571477850876_70164200470120'

I generated the html files shown in the routes.rb file below; the html files are located in app/views/page

Routesapp::Application.routes.draw do
  get 'page/home'

  get 'page/about'

  get 'page/contact'

Any ideas, comments, suggestions will be much appreciated. This is new to me. Thanks

Upvotes: 3

Views: 482

Answers (2)

S.Pavitrakar
S.Pavitrakar

Reputation: 1

Buddy take "application" on line #5 and #6 and change that "application" word to "defaults"

Upvotes: 0

Richard Peck
Richard Peck

Reputation: 76774

Since you're a beginner, let me explain.

Your pages are showing; you've got an error:

undefined method `[]' for nil:NilClass

This is a standard Ruby error - it means undeclared variable.

Since Ruby is Object Orientated, it assigns "nil" values to the NilClass -- meaning that you end up with strange errors.

--

Normally, with debugging, you'll be able to reference the line number, and see what the issue is.

Because you've not included your view, I can only suggest that you try removing the following lines as a test:

5:   <%= stylesheet_link_tag    "application", :media => "all" %>
6:   <%= javascript_include_tag "application" %>

If these work to resolve the issue, you'll have to either populate app/assets/javascripts/application.js or app/assets/stylesheets/application.css, or use something like nodeJS which works to resolve errors like this.


You'll also be better using the following for your routes:

#config/routes.rb
resources :pages, path: "page", only: [] do
   %w(home about contact).each do |page|
      get page
   end
end

Upvotes: 2

Related Questions