Ruby on Rails url更改但视图无法加载

时间:2016-08-21 22:37:53

标签: ruby-on-rails ruby routing

我已经研究了Ruby on Rails几个月了,我正在第二次进行Hartl教程以进行额外练习。在开发和生产环境中,所有指向我网站上根网址的链接都会在浏览器中放置正确的网址,但不会加载视图。只有在单击其中一个指向根URL的链接后点击刷新按钮时才会加载正确的视图。我认为这可能是turbolinks gem的一个问题,但我不确定。

在单击指向根URL的链接后查看服务器日志时,不会显示任何错误消息,只会显示静态页面/部分的日志,并跟踪用户/微博信息。此外,当我点击刷新按钮并正确加载视图时,服务器日志与我第一次单击任何指向根URL的链接完全相同。

我认为如果我只是链接到我的git存储库而不是发布单个文件,那么每个人都会更容易看到我可能错过的内容:https://bitbucket.org/corourke24/sample_app2

非常感谢任何建议或帮助,谢谢!

编辑:MarsAtomic建议如果我展示链接不起作用的示例会更容易。根URL的主要链接位于标题部分:

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <%= link_to "Caturday Surprise!", 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>
        <% if logged_in? %>
          <li><%= link_to "Users", users_path %></li>
          <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 %></li>
              <li><%= link_to "Settings", edit_user_path(current_user) %></li>
              <li class="divider"></li>
              <li>
                <%= link_to "Log out", logout_path, method: "delete" %>
              </li>
            </ul>
         </li>
        <% else %>
          <li><%= link_to "Log in", login_path %></li>
        <% end %>
      </ul>
    </nav>
  </div>
</header>

这是我的路线文件

Rails.application.routes.draw do

  get 'password_resets/new'

  get 'password_resets/edit'

  get 'sessions/new'


  root 'static_pages#home'
  get    '/help',     to: 'static_pages#help'
  get    '/about',    to: 'static_pages#about'
  get    '/contact',  to: 'static_pages#contact'
  get    '/signup',   to: 'users#new'
  post   '/signup',   to: 'users#create'
  get    '/login',    to: 'sessions#new'
  post   '/login',    to: 'sessions#create'
  delete '/logout',   to: 'sessions#destroy'
  resources :users do
    member do
      get :following, :followers
    end
  end
  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]
  resources :microposts,          only: [:create, :destroy]
  resources :relationships,       only: [:create, :destroy]
end

这是我的static_pages / home文件(root url):

<% if logged_in? %>
    <%= render 'shared/logged_in_home' %>
<% else %>
    <%= render 'shared/logged_out_home' %>
<% end %>

以下是它呈现的部分内容:

logged_in_home:

<div class="row">
    <aside class="col-md-4">
        <section class="user_info">
            <%= render 'shared/user_info' %>
        </section>
        <section class="stats">
            <%= render 'shared/stats' %>
        </section>
        <section class="micropost_form">
            <%= render 'shared/micropost_form' %>
        </section>
    </aside>
    <div class="col-md-8">
        <h3>Micropost feed</h3>
        <%= render 'shared/feed' %>
    </div>
</div>

logged_out_home:

<div class="center jumbotron">
    <h1>Caturday Surprise</h1>

    <h2>
        This is the home page for 
        <a href="http://cat-bounce.com/">Cats!</a>
        (a sample application).
    </h2>
        <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>
<div class="center">
    <%= link_to image_tag("kitten.jpg", alt: "Kitten picture",
                           title: "All cats matter"),
                            'http://iheartcats.com/' %>

    <%= link_to image_tag("cute-kitten.jpg", alt: "Cute kitten picture", 
                          title: "Random kittens generator"), 
                          'http://www.randomkittengenerator.com/' %>
</div>

这是logged_in?我的sessions_helper文件中的方法:

def logged_in?
    !current_user.nil?
end

0 个答案:

没有答案