-1

My links aren't working for my ruby on rails site. When I click the link the site stays on the same page. I'm following code from a book called: "RailsSpace: Building a social networking site with ruby on rails" By Michael Hartl. The book is available free for download you can follow the code for yourself, its on pages 55-56 starting under the title "adding navigation". Here is a link to the ebook.

Thanks for your help.

I tried googling the "link_to" function and changing the format, all it did was generate more errors please help.

Here is the code from my app/views/layout/site.html.erb file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html>
    <head>
      <title><%= @title %></title>
      <%= stylesheet_link_tag "site" %>
    </head>
    <body>
      <div id="whole_page">
        <div id="header">RailsSpace</div>
        <div id="nav">
          <%= link_to_unless_current "Home", :action => "index" %> |
          <%= link_to "About Us" , :action => "help" %> |
          <%= link_to_unless_current "Help", :action => "help" %>
          <div style="background-color:white">
            <h1>Welcome to RailSpace!</h1>
            <p>This is going to be the best site ever!</p>
          </div>
        </div>
        <div id="content">
          <%= @content_for_layout %>
        </div>
      </div>
    </body>
  </html>

Here is the code for my app/controllers/site_controller.rb file:

class SiteController < ApplicationController

  def index
    @title = "Welcome to RailsSpace!"
  end

  def about
    @title = "About RailsSpace"
    @header = "Hello"
  end

  def help
    @title = "RailsSpace Help"
  end

end

Here is the code for my config/routes.rb file:

Rails.application.routes.draw do
  get  'site/index'
  get '/site/about'
  get 'site/help'

  root  'site#index'

// Here is the output from the console when I click on a link as requested:

 Started GET "/site/about" for ::1 at 2019-10-01 15:28:48 -0700
 Processing by SiteController#about as HTML
   Rendering site/about.html.erb within layouts/site
   Rendered  site/about.html.erb within layouts/site (Duration: 0.0ms | 
   Allocations: 5)
   Completed 200 OK in 7ms (Views: 6.8ms | ActiveRecord: 0.0ms | 
   Allocations: 2921)

Thanks for all your help

When I click on the links in my navbar the site stays on the same page. Please help.

  • 1
    Possible duplicate of [Why are my links not working for my ruby on rails site? Links stay on same page](https://stackoverflow.com/questions/58191812/why-are-my-links-not-working-for-my-ruby-on-rails-site-links-stay-on-same-page) – jvillian Oct 01 '19 at 21:53
  • I reposted the same question so I could include code from two more files. I didn't know how to edit the question I already had. – Joshua Matthews Oct 01 '19 at 22:02
  • There is an `edit` link right below the tags. – jvillian Oct 01 '19 at 22:03
  • Lol OMG I did not see that, I expected it to be all shiny and blue. Thanks. – Joshua Matthews Oct 01 '19 at 22:07
  • Please add to your question the output from your console when you click on a link. – jvillian Oct 01 '19 at 22:10
  • Hey thanks, I just re-edited the post and included the output from console when I click on links. Thanks so much for your help. – Joshua Matthews Oct 01 '19 at 23:01
  • Hey thanks, I just re-edited the post and included the output from console when I click on links. Thanks so much for your help. – Joshua Matthews Oct 01 '19 at 23:02
  • If you inspect the link element what do you see in the generated HTML? Does it fix it if you remove `<%= @content_for_layout %>` and replace it with `<%= yield %>` ? – Rockwell Rice Oct 01 '19 at 23:58
  • Yep thanks man. That actually fixed it. I really apreciate all of your help. Thanks so much. – Joshua Matthews Oct 02 '19 at 00:17

1 Answers1

0

Thanks To Mr. Rockwell Rice, he suggested that I replace <%= @content_for_layout %> with <%= yield %> and it actually worked. All praise goes to Mr. Rockwell Rice. Thanks alot man.