【问题标题】:why are my links not working?为什么我的链接不起作用?
【发布时间】:2013-05-25 09:17:48
【问题描述】:

我刚刚将我的网站从本地 webrick 服务器部署到了真实服务器,

http://92.51.243.6/

我很高兴自己设法部署了它,但是当我意识到我的大部分链接都不起作用时,这种感觉很快就消失了。 (我知道 Facebook 的问题,这真的是我现在关心的链接) - 关于我们、常见问题解答、联系我们,以及在“注册”页面上,链接“条款和条件”给出了错误,“我“抱歉,出了点问题”

我用代码建立了链接:

 <div id = "menu">
      <ul id = "home_page_links">
        <li><%= link_to "Home",about_us_path, :remote => true %></li>
        <li><%= link_to "About Us",about_us_path, :remote => true  %></li>
        <li><%= link_to "FAQ", faq_path, :remote => true  %></li>
        <li><%= link_to "Contact Us", contact_us_path, :remote => true  %></li>
      </ul>
    </div>

所有链接在本地模式下都可以在 WEBrick 上正常工作。在我的生产日志中,我收到了很多消息,例如:

Started GET "/contact_us" for 77.24.238.174 at Thu May 30 00:11:08 +0100 2013
Processing by StaticPagesController#contact_us as */*
  Rendered static_pages/contact_us.html.erb within layouts/application (0.0ms)
Completed 500 Internal Server Error in 4ms

ActionView::Template::Error (ie.css isn't precompiled):
    12:   <%= javascript_include_tag "application" %>
    13:   <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    14:   <!--[if lt IE 9]>
    15:     <%= stylesheet_link_tag 'ie', :media => 'all' %>
    16:     <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    17:   <![endif]-->
    18: </head>
  app/views/layouts/application.html.erb:15:in `_app_views_layouts_application_html_erb__922519344_37417300'

这是我的 routes.rb 文件:

QuestionnaireSite::Application.routes.draw do

  get "about_us", :to => "static_pages#about_us"
  get "contact_us", :to => "static_pages#contact_us"
  get "faq", :to => "static_pages#faq"
  get "competition_terms", :to => "static_pages#competition_terms"
  get "t_and_c", :to => "static_pages#terms_and_conditions"
  # get "user", :to => @user(current_user)
  get "render_index_review", :to => "reviews#render_index"

  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

  devise_scope :user do
    delete "sign_out", :to => "devise/sessions#destroy"
    get "login", :to => "devise/sessions#new"
  end

  devise_for :admins, :path => "admin", :controllers => { :sessions => "admin/sessions" }

  get "home/index"
  get "home/map", :as  => :map
  post "home/update_address", :as  => :update_address
  match "canvas" => "canvas#index"

  match "/contacts/:importer/callback" => "email_invites#contacts_callback", :as => :contacts_callback
  match "/contacts/failure" => "email_invites#contacts_callback_failure", :as => :contacts_callback_failure
  mount Resque::Server, :at => "/resque"

  get "find" => 'search#index', :as => :find

  resources :search do
    collection do
      match :change_range
    end
  end

  resources :reviews do
    member do
      get :repost, :reject
    end
  end
  resources :users do
    member do
      get :following_followers, :address_toggle
    end
  end
  resources :friend_relations, :only => [:create, :destroy]
  resources :email_invites do
    collection do
      get :confirm
      post :invitation_form
      post :outlook_import
    end
  end

  root :to => "home#index"

  namespace :admin do
    resource :profile, :only => [:edit, :update]

    resources :users
    resources :categories
    resources :reviews

    root :to => "users#index"
  end

还有我的 scripts.js 文件,它执行 ajax 的东西:

//I want to load content into the '.page-content' class, with ajax

var ajax_loaded = (function(response) {        

  $(".page-content")

  .html($(response).filter(".page-content"));       

  $(".page-content .ajax").on("click",ajax_load); 



   });


//the function below is called by links that are described 
//with the class 'ajax', or are in the div 'menu' 

var history = [];                 

// var current_url_method;               

var ajax_load = (function(e) {  


  //console.log('load ajax on clicks. This always works.');         
  e.preventDefault();               


  history.push(this);               
  var url =$(this).attr("href");          
  var method = $(this).attr("data-method");   

  // if (current_url_method != url + method) {   
  //   console.log('You got to the url + method part. But sometimes I dont get this far.'); 
  //   current_url_method = url + method;      

  $.ajax({                  
    url: url,               
    type: method,  
    // async: false,                       
    success: ajax_loaded

    // $('html, body').animate({ scrollTop: 0 }, 0);       
  });
   // }

 });


//monitor for clicks from menu div, or with
//the ajax class, or the 'submit button'.
//why the trigger?

$(document).on("ready", function(){
$("#menu a").on("click",ajax_load);
$(".ajax").on("click",ajax_load);
$("#menu a.main").trigger("click");


});

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • production.log 文件或任何 http 日志文件中没有任何内容?
  • 如果您向我们提供有关您如何建立链接的更多信息,我们将不胜感激。
  • 嗨,我直到现在才知道 production.log(部署新手)。 production.log 到底是做什么的?我在终端中看到它在我单击链接时发生变化。我使用 production.log 中的一个 sn-ps 代码编辑了上面的问题。如何修复内部服务器错误?如何预编译ie.css?谢谢。
  • 您是否预编译了资产?看起来你没有。运行 rake assets:precompile 在服务器上。

标签: ruby-on-rails git deployment


【解决方案1】:

您是否在生产环境中部署您的应用程序?

错误是:

ActionView::Template::Error(ie.css 未预编译):

这意味着您需要预编译所有资产。请运行此命令并重新启动您的服务器。

bundle exec rake assets:precompile

【讨论】:

    【解决方案2】:

    您是否在生产环境中部署您的应用程序?

    错误是: ActionView::Template::Error(ie.css 未预编译):

    这意味着您需要预编译所有资产。请运行此命令并重新启动您的服务器。

    bundle install
    bundle exec rake assets:clean
    bundle exec rake assets:precompile
    

    如需了解更多脱轨,请按照 rails deploy guide 中所述的这些基本步骤进行操作。 http://guides.rubyonrails.org/asset_pipeline.html#in-production

    【讨论】:

    • 感谢您的提示,但没有像我希望的那样工作。有人说将 *= require_tree 放入 application.css.scss 并重新部署,所以我会试一试。
    • *= require_tree 将添加每个 css 文件。我以为你已经在 appliation.css.scss 文件中写了。
    • 我从别人那里继承了这个 Rails 项目。而且我当然不是rails专家。但是在我的application.css.scss文件中,我看到*=要求同一文件夹中的每个scss文件,除了ie.css.scss。这可能是问题吗?我的意思是,只需添加 *= require ie 并重新部署?
    • 你可以出去..这就是问题
    【解决方案3】:

    好的,我会一步一步地向你解释如何在 Rails 中执行 ajax 请求,至少这是我的做法,我不会强迫你这样做。

    1.链接和路径

    <%= link_to "FAQ", faq_path, :remote => true  %>
    

    remote: true设置为链接意味着请求将被视为ajax。

    路径faq_path 表示您在routes.rb 中定义了它(如您的情况:get "faq", :to =&gt; "static_pages#faq"

    2。控制器和操作

    将您的路由设置为get "faq", :to =&gt; "static_pages#faq" 意味着您有一个名为static_pages 的控制器和一个名为faq 的内部操作

    def faq
      may be some code here but not necessary for static pages
    end
    

    3.视图和 js.erb

    就目前而言,您可能在app/views/static_pages/ 中有一个faq.html.erb,并且此页面适用于html 请求,但是当我们以ajax 执行此操作时,它将查找faq.js.erb,这就是您执行javascript 的地方。

    首先我会将您的faq.html.erb 内容移动到部分_faq_content.html.erb 中,在faq.html.erb 中我只会将其呈现为部分,因为我们将使用相同的部分以防万一将是一个 html 请求(适用于在浏览器中禁用 js 的用户)

    现在我承认,当单击常见问题链接时,您希望将page-content div 中的内容替换为常见问题页面的内容。为此,请在您的faq.js.erb(如果没有,请创建一个)中写:

    $('.page-content').html('<%= j render('faq_content') %>');
    

    如果您需要,可以在此处添加更多 js 代码,例如为菜单中的链接设置带下划线的样式,或者您可以对该页面上的任何元素执行任何操作。

    点击&lt;div class="page-content"&gt;&lt;/div&gt;的faq链接内容后,将替换为faq部分内容的内容。

    我建议您在尝试我的方法时删除(注释)您的 js 代码,顺便说一下,Chrome 在单击链接时会显示错误:

    GET http://92.51.243.6/about_us 500 (Internal Server Error) application-
    GET http://92.51.243.6/faq 500 (Internal Server Error) application-
    GET http://92.51.243.6/contact_us 500 (Internal Server Error) 
    

    所以你最好在尝试我的教程时删除你的 js :) 只是为了确保不会有冲突。如果你做的很好,你应该为你的静态页面获得一个 ajax 渲染。如果您对此有任何疑问,请告诉我。祝你好运。

    【讨论】:

    • 干杯 rmagnum,感谢您的帮助。很快就会调查它。这肯定与我认为的 ajax 方式不同!如果它有效,它会更干净、更短。 “提交”按钮可能有问题,但当我谈到它时,我会担心。我已将其标记为 1,如果有效,我将接受您的回答。
    • 提交按钮没什么难的,您将在表单中添加 remote: true 并且您在该表单中执行的操作将与 ajax 一起使用,就像链接一样,例如,如果您执行创建用户表单, 这样用户控制器中的创建操作将响应 js,这意味着您需要有 app/views/users /create.js.erb 并在其中添加所需的 js。但正如你所说,当你得到它时,你会担心它。
    猜你喜欢
    • 2015-06-24
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多