2014-04-08 112 views
0

您正在查找的頁面不存在。您可能錯誤輸入了地址,或者頁面可能已移動。Ruby錯誤ActiveRecord :: RecordNotFound

錯誤日誌:

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 

可能是什麼問題呢? 一個程序員下班後停止了所有的工作,他正在做一些緩存。

+0

你在頁表中有':id = 4'的記錄嗎? – Pavan

回答

1

問題是在這裏

@page = Page.find(2) 

而不是使用查找,使用其中

@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 
+0

要嵌入哪行? 可以請提供一個現成的代碼? – user3509507

+0

如果您正在使用'find',請檢查我的更新回答 – RSB

+0

通過搜索沒有找到 可以請給一個現成的代碼片? – user3509507

相關問題