我是Ruby和Rails的新手,我继承了一个现有的公司网站,我一直在修改这个网站以找到一个新的工作跟踪网站。一切都在开发中很好,但是当我部署到我们的登台服务器时,我的两条工作路线开始出现问题。
在我更详细地描述之前,我在AWS Linux服务器上使用以下版本:
我有两条导致问题的路线:查看职位发布和申请工作。有时,作业发布会显示在两个URL上,有时两个URL都显示作业应用程序,我认为它最终会显示首先加载的页面。
与jobs_controller相关的routes.rb中的行是(添加注释以解释URL):
# Submits a general application to the company
get '/careers/apply', controller: :jobs, action: :apply, as: :apply
# Applies to a particular position
get '/career/:ref_number/apply', controller: :jobs, action: :apply, as: :apply_job
# Displays details for a particular job opening
get '/career/:ref_number', controller: :jobs, action: :show, as: :job
# Searches available job openings
get '/careers/search', controller: :jobs, action: :search, as: :job
我尝试重新排序文件中的路由,更改网址以使它们不重叠并更改HTTP方法,但没有任何工作。
jobs_controller.rb:
require 'pagination'
require 'related'
require 'job_selector'
class JobsController < ApplicationController
include Website::Pagination
before_filter :set_page_or_redirect, only: :index
skip_authorization_check
def index
@jobs = Job.with_tag_associations.page(@page)
end
def show
@job = Job.find_by_ref_number(params[:ref_number])
related_job_finder = Website::Related.new(@job)
@related_jobs = {
tags: related_job_finder.related(Job, :tags).limit(5).to_a,
categories: related_job_finder.related(Job, :categories).limit(5).to_a,
}
@featured_jobs = Website::JobSelector.random_featured_jobs(2)
end
def search
@location = params[:location]
@query = params[:query]
@search = Job.solr_search(@query, params.slice(:location, :category, :page))
@jobs = @search.results
end
def apply
if !params[:ref_number].nil?
@job = Job.find_by_ref_number(params[:ref_number])
related_job_finder = Website::Related.new(@job)
@related_jobs = {
tags: related_job_finder.related(Job, :tags).limit(5).to_a,
categories: related_job_finder.related(Job, :categories).limit(5).to_a,
}
else
@job = Job.new(ref_number: -1,
title: 'General Application')
@related_jobs = {
tags: [],
categories: []
}
end
@featured_jobs = Website::JobSelector.random_featured_jobs(2)
end
end
我的staging.log文件在查看作业申请时显示以下内容,然后单击“应用”按钮以查看应用程序页面。根据日志,调用正确的控制器方法并为每个页面加载正确的views / *组件,但是 - 在这种情况下 - 两个URL都显示了作业详细信息,我无法访问作业应用程序。
查看工作页面(GET /career/70779
)
Started GET "/career/70779" for 173.66.87.200 at 2014-02-27 04:04:42 +0000
Processing by JobsController#show as HTML
Parameters: {"ref_number"=>"70779"}
Job Load (0.5ms) SELECT `jobs`.* FROM `jobs` WHERE `jobs`.`ref_number` = 70779 LIMIT 1
ActsAsTaggableOn::Tag Load (0.5ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
Job Load (1.5ms) SELECT jobs.*, COUNT(tags.id) AS count FROM jobs, tags, taggings WHERE (jobs.id != 261 AND jobs.id = taggings.taggable_id AND taggings.taggable_type = 'Job' AND taggings.tag_id = tags.id AND tags.name IN (NULL)) GROUP BY jobs.id ORDER BY count DESC LIMIT 5
ActsAsTaggableOn::Tag Load (0.3ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'categories' AND taggings.tagger_id IS NULL)
CACHE (0.0ms) SELECT jobs.*, COUNT(tags.id) AS count FROM jobs, tags, taggings WHERE (jobs.id != 261 AND jobs.id = taggings.taggable_id AND taggings.taggable_type = 'Job' AND taggings.tag_id = tags.id AND tags.name IN (NULL)) GROUP BY jobs.id ORDER BY count DESC LIMIT 5
(24.7ms) SELECT COUNT(*) FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'primary_category')
ActsAsTaggableOn::Tag Load (0.6ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'primary_category') LIMIT 1
Read fragment views/jobs/261-20140226035339 (0.2ms)
CACHE (0.0ms) SELECT COUNT(*) FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'primary_category')
CACHE (0.0ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'primary_category') LIMIT 1
Rendered shared/_category_label.html.erb (8.5ms)
Rendered shared/share_links/_twitter.html.erb (0.4ms)
Rendered shared/share_links/_facebook.html.erb (0.4ms)
Rendered shared/share_links/_google_plus.html.erb (0.4ms)
Rendered shared/share_links/_linkedin.html.erb (0.3ms)
Rendered jobs/_share_links.html.erb (29.6ms)
(0.4ms) SELECT COUNT(*) FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'tags')
Rendered shared/_tags.html.erb (4.6ms)
Rendered jobs/_apply_button.html.erb (1.0ms)
Read fragment views/jobs/261-20140226035339/join-us (0.1ms)
Job Load (1.4ms) SELECT `jobs`.* FROM `jobs` WHERE `jobs`.`is_featured` = 1 ORDER BY RAND() LIMIT 2
Read fragment views/jobs/263-20140226190437/sidebar (0.1ms)
Write fragment views/jobs/263-20140226190437/sidebar (4.1ms)
Read fragment views/jobs/262-20140226164039/sidebar (0.1ms)
Write fragment views/jobs/262-20140226164039/sidebar (27.2ms)
Rendered shared/_show_sidebar.html.erb (178.1ms)
Write fragment views/jobs/261-20140226035339/join-us (2.8ms)
Rendered jobs/_side_bar.html.erb (184.0ms)
Write fragment views/jobs/261-20140226035339 (2.6ms)
Rendered jobs/show.html.erb within layouts/application (386.9ms)
Rendered layouts/_head.html.erb (3.4ms)
Read fragment views/navigation (0.1ms)
Write fragment views/navigation (2.8ms)
Rendered layouts/_navigation.html.erb (5.2ms)
Rendered layouts/_context_nav.html.erb (0.6ms)
Rendered layouts/_flash_messages.html.erb (24.9ms)
Rendered layouts/_javascripts.html.erb (4.3ms)
Rendered layouts/_footer.html.erb (31.7ms)
Completed 200 OK in 1860ms (Views: 683.4ms | ActiveRecord: 34.6ms | Solr: 0.0ms)
申请职位页面(GET /career/70779/apply
)
Started GET "/career/70779/apply" for 173.66.87.200 at 2014-02-27 04:04:56 +0000
Processing by JobsController#apply as HTML
Parameters: {"ref_number"=>"70779"}
Job Load (0.5ms) SELECT `jobs`.* FROM `jobs` WHERE `jobs`.`ref_number` = 70779 LIMIT 1
ActsAsTaggableOn::Tag Load (0.4ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
Job Load (1.0ms) SELECT jobs.*, COUNT(tags.id) AS count FROM jobs, tags, taggings WHERE (jobs.id != 261 AND jobs.id = taggings.taggable_id AND taggings.taggable_type = 'Job' AND taggings.tag_id = tags.id AND tags.name IN (NULL)) GROUP BY jobs.id ORDER BY count DESC LIMIT 5
ActsAsTaggableOn::Tag Load (0.3ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'categories' AND taggings.tagger_id IS NULL)
CACHE (0.0ms) SELECT jobs.*, COUNT(tags.id) AS count FROM jobs, tags, taggings WHERE (jobs.id != 261 AND jobs.id = taggings.taggable_id AND taggings.taggable_type = 'Job' AND taggings.tag_id = tags.id AND tags.name IN (NULL)) GROUP BY jobs.id ORDER BY count DESC LIMIT 5
(0.4ms) SELECT COUNT(*) FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'primary_category')
ActsAsTaggableOn::Tag Load (0.4ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 261 AND `taggings`.`taggable_type` = 'Job' AND (taggings.context = 'primary_category') LIMIT 1
Read fragment views/jobs/261-20140226035339 (0.4ms)
Rendered jobs/apply.html.erb within layouts/application (6.6ms)
Rendered layouts/_head.html.erb (130.7ms)
Read fragment views/navigation (0.3ms)
Rendered layouts/_navigation.html.erb (0.7ms)
Rendered layouts/_context_nav.html.erb (0.0ms)
Rendered layouts/_flash_messages.html.erb (0.1ms)
Rendered layouts/_javascripts.html.erb (2.6ms)
Rendered layouts/_footer.html.erb (3.5ms)
Completed 200 OK in 156ms (Views: 144.2ms | ActiveRecord: 3.1ms | Solr: 0.0ms)
我的部署脚本执行以下操作:
RAILS_ENV=staging bundle exec rake db:migrate
RAILS_ENV=staging bundle exec rake assets:precompile
touch tmp/restart.txt
RAILS_ENV=staging bundle exec rake sunspot:reindex
配置/环境/ staging.rb:
NewWebsite::Application.configure do
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Do fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Turn on memory caching (tried this after seeing some SO answers; didn't make a difference)
config.cache_store = :memory_store
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
最后一个细节。我在预编译资产后运行RAILS_ENV=staging rails s
时能够在本地复制问题,所以我认为它与Apache / Passenger和本地执行无关。
提前致谢!
答案 0 :(得分:0)
所以,我刚刚学到了很多关于Rails片段缓存的教训!
我遇到了这个问题,因为我复制了负责显示作业详细信息的show.html.erb
文件,并对其进行了编辑,使apply.html.erb
文件显示了实际应用程序的IFRAME。我没有意识到它在做什么,我在apply.html.erb
文件中留下了以下一行:
<% cache @job do %>
我现在已经意识到启用Rails片段缓存,这解释了我的所有问题:
development
中禁用了缓存,并在staging
和production
中启用了缓存,这就是此问题从未在本地出现的原因。<% cache @job do %>
块在两个片段中都是相同的,因此它解释了为什么我在两个网址中的任何给定作业中首先看到的页面中的内容。解决方案:
从应用程序片段中删除了cache
块,因为它的处理可以忽略不计。 Rails现在每次加载时都会适当地生成该页面,同时仍然缓存作业详细信息显示页面。