与establish_connection有关的问题“#{Rails.env}

时间:2018-04-04 22:14:31

标签: ruby-on-rails database import

我遇到了Rails的问题。

我在Ruby on Rails中创建了一个站点,它显示了另一个站点中的帖子,这些站点也是在Rails中创建的。在本地版本中它可以很好地工作,但是当我尝试在线版本时,它会中断并在服务器上的站点日志中显示以下错误:

ActionView::TemplateError (undefined method 'slug' for nil:NilClass).

有人能给我一个如何解决它的提示吗?

Post.rb

  # -*- encoding : utf-8 -*-
  class Post < ActiveRecord::Base
  establish_connection "#{Rails.env}_facens_br".to_sym
  self.per_page = 3
  include TitleSluggable
  belongs_to :axis

  default_scope { where(website: 'smartcampus').order(published_at: :desc) }
  scope :featured,     -> { where(featured: true)     }
  scope :not_featured, -> { where.not(featured: true) }

  validates :title, :axis, :content, presence: true
  validate :video_or_image

  has_attached_file :image, styles: { thumb: '200x125#', display: '400x250#' }
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/

  def youtube_id
     regex = /(?:.be\/|\/watch\?v=|\/(?=p\/))([\w\/\-]+)/
     self.video.match(regex)[1]
  end

  def video
    self.link
  end

  private

    def video_or_image
      if self.image.blank? && self.video.blank?
        errors.add(:base, "Por favor, insira um vídeo ou uma imagem.")
      end
    end
end

Posts_controller.rb

# -*- encoding : utf-8 -*-
class PostsController < ApplicationController
  def index
    @posts_featured = Post.all.order('featured desc').limit(2)

    if params[:eixo].present? or params[:q].present?
      @posts = []

      by_query = []
      by_axis = []

      if params[:q].present?
        by_query = Post.where("title LIKE ? OR content like ?", "%#{params[:q]}%", "%#{params[:q]}%")
                       .paginate(:page => params[:page])
                       .order(published_at: :desc)
      end

      if params[:eixo].present?
        axis    = Axis.find(params[:eixo])
        by_axis = Post.where(axis_id: axis.id)
                      .paginate(:page => params[:page]).order('published_at desc')
                      .order(published_at: :desc)
      end

      @posts = (by_query + by_axis).flatten.uniq
    else
      @posts = Post.where(featured: false).paginate(:page => params[:page]).order('published_at desc')

    end
    @axis  = Axis.all.joins(:posts).uniq
  end

  def show
    @post  = Post.find(params[:slug])
    @posts = Post.where("axis_id = ? AND id != ?", @post.axis, @post.id).order(published_at: :desc).limit(5)
  end
end

查看:

<% @posts.each do | post | %>
  <a href="http://www.facens.br/noticias/smartcampus/<%= post.slug %>" target="_blank" class="item-noticia">
    <div class="img-noticia">
    <div class="overlay b-<%= post.axis.slug %>"><%= image_tag('iconplus.png') %></div>
      <%= image_tag_from_facens(post.image.url(:display)) %>
    </div>
    <div class="txt-noticia">
      <div class="tit-noticia t-<%= post.axis.slug %>"><%= post.title %></div>
      <p><%= raw(strip_tags(post.content)[0..80]) %>..</p>
    </div>
  </a>
<% end -%>

的database.yml

development:
  adapter: mysql2
  encoding: utf8
  database: smartfacens_development

test:
  adapter: mysql2
  encoding: utf8
  database: smartfacens_test

production:
  adapter: mysql2
  encoding: utf8
  database: smartfacens_development

##################################
# SITE DA FACENS
##################################
development_facens_br:
  adapter: mysql2
  encoding: utf8
  database: facens_development
  username: root
  password: 

production_facens_br:
  adapter: mysql2
  database: facens_br
  username: root
  password:
  port: 3306

0 个答案:

没有答案