Why are my links not working for my ruby on rails site? Links stay on same page when clicked on

时光毁灭记忆、已成空白 提交于 2019-12-03 00:11:12

问题


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:


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.



来源:https://stackoverflow.com/questions/58192739/why-are-my-links-not-working-for-my-ruby-on-rails-site-links-stay-on-same-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!