简体   繁体   中英

Ruby Error ActiveRecord::RecordNotFound

The page you were looking for doesn't exist. You may have mistyped the address or the page may have moved.

Error log:

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

What could be the problem? Stopped all work after work of a programmer, he was doing something with the cache.

Problem is here

@page = Page.find(2)

Instead of using find, use where

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

Then you can apply a check like

if @page.blank?
  #do something
else
  #do something else
end

Here's the update

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM