Ruby Error ActiveRecord :: RecordNotFound

时间:2014-04-08 05:29:28

标签: ruby-on-rails ruby

您要查找的页面不存在。您可能输错了地址或页面可能已移动。

错误日志:

Started GET "/ru" for 0.0.0.0 at 2014-04-08 08:03:02 +0300
  Processing by PagesController#main as HTML
  Parameters: {"locale"=>"ru"}
Completed 404 Not Found in 14ms

ActiveRecord::RecordNotFound (Couldn't find Page with id=4):
  app/controllers/pages_controller.rb:9:in `main'

pages_controller:

class PagesController < ApplicationController

  before_filter :authenticate, :only => [:index, :show, :new, :edit, :create, :update, :destroy ]
  caches_page :main, :about, :showreal, :projects, :contacts, :reject
  caches_action :index, :show, :new

  def main    
    @page = case I18n.locale.to_s
      when 'ru'; Page.find(4)
      when 'zh'; Page.find(6)  
      else Page.find(2)
    end
    base = 'app/assets/videos/'    
    @video = Dir.glob(File.join(base, 'main', 'video.*')).first.sub!(base,"")    
    @preview = Dir.glob(File.join(base, 'main', 'preview.*')).first.sub!(base,"")
  end

  def about
    @body_class = 'leftbg'
    @page = case I18n.locale.to_s
      when 'ru'; Page.find(3)
      when 'zh'; Page.find(5)  
      else Page.find(1)
    end
  end

  def showreal
    base = 'app/assets/videos/'    
    @video = Dir.glob(File.join(base, 'showreal', 'video.*')).first.sub!(base,"")    
    @preview = Dir.glob(File.join(base, 'showreal', 'preview.*')).first.sub!(base,"")
  end

  def projects   
  end

  def contacts
    @body_class = 'leftbg'
  end

  def sent
    redirect_to :action => "reject" and return unless request.post?

    @body_class = 'leftbg'

    if request.POST.include? 'name'
      @name = request.params['name']
    end
    if request.POST.include? 'email'
      @email   = request.params['email']
    end
    if request.POST.include? 'phone'
      @phone   = request.params['phone']
    end
    if request.POST.include? 'message'
      @message = request.params['message']
    end

    if ['name','email','phone','message'].all? {|i| request.POST.include?(i)}
      ContactMailer.contacts_email(@name,@email,@phone,@message).deliver
    else
      flash[:error] = 'You must complete all fields!'
      return render :action => "contacts"
    end
  end

  def reject
    @body_class = 'leftbg'
  end  

  # GET /pages
  # GET /pages.json
  def index
    @pages = Page.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @pages }
    end
  end

  # GET /pages/1
  # GET /pages/1.json
  def show
    @page = Page.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @page }
    end
  end

  # GET /pages/new
  # GET /pages/new.json
  def new
    @page = Page.new
    expire_page :action => [:main, :about]
    expire_action :action => [:index, :show]

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @page }
    end
  end

  # GET /pages/1/edit
  def edit
    @page = Page.find(params[:id])
  end

  # POST /pages
  # POST /pages.json
  def create
    @page = Page.new(params[:page])

    respond_to do |format|
      if @page.save
        format.html { redirect_to @page, notice: 'Page was successfully created.' }
        format.json { render json: @page, status: :created, location: @page }
      else
        format.html { render action: "new" }
        format.json { render json: @page.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /pages/1
  # PUT /pages/1.json
  def update
    @page = Page.find(params[:id])
    expire_page :action => [:main, :about]
    expire_action :action => [:index, :show] 

    respond_to do |format|
      if @page.update_attributes(params[:page])
        format.html { redirect_to @page, notice: 'Page was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @page.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /pages/1
  # DELETE /pages/1.json
  def destroy
    @page = Page.find(params[:id])
    @page.destroy
    expire_page :action => [:main, :about]
    expire_action :action => [:index, :show]

    respond_to do |format|
      format.html { redirect_to pages_url }
      format.json { head :ok }
    end
  end
end

可能是什么问题? 在程序员工作之后停止了所有工作,他正在使用缓存做一些事情。

1 个答案:

答案 0 :(得分:1)

问题在这里

@page = Page.find(2)

不使用find,而是使用

@page = Page.where("id =?", 2).first

然后你可以应用像

这样的支票
if @page.blank?
  #do something
else
  #do something else
end

这是更新

@page = case I18n.locale.to_s
  when 'ru'; Page.where("id =?", 4).first
  when 'zh'; Page.where("id =?", 6).first
  else Page.where("id =?", 6).first
end