Ruby Error PagesController #main

时间:2014-04-08 06:41:58

标签: ruby-on-rails ruby

网站链接xxx.com/ru不起作用

错误日志:

  Processing by PagesController#main as HTML
  Parameters: {"locale"=>"ru"}
Rendered pages/main.html.erb within layouts/application (27.7ms)
Completed 500 Internal Server Error in 46ms

ActionView::Template::Error (undefined method `title' for nil:NilClass):
    19:
    20:       <article class="hello">
    21:         <dl>
    22:           <dt><%= @page.title %></dt>
    23:           <dd><%=raw nl2br @page.content.html_safe %></dd>
    24:         </dl>
    25:       </article>
  app/views/pages/main.html.erb:22:in `_app_views_pages_main_html_erb___1446230423625798988_17243439220'

如果您继续链接&#34; xxx.com/ru/pages#main",那么它可以正常工作

UPD:现在只有工作/ ru,/ en - 不起作用 错误:

  Processing by PagesController#main as HTML
  Parameters: {"locale"=>"en"}
Completed 404 Not Found in 1ms

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

可能是什么问题?我请求你帮忙解决它,网站应该很快打开,他不是全部工作,一个程序员搞砸了代码。 控制器文件:

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.where("id =?", 4).first
      when 'zh'; Page.where("id =?", 6).first
      else Page.where("id =?", 6).first
    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 :(得分:0)

我相信@page为零,所以请尝试:

<%= @page.try(:title) %>

或:

<dt><%= @page.title if @page.present?%></dt>
<dt><%= raw nl2br @page.content.html_safe if @page.content.present? if @page.present? %></dt>

或者,在控制器的操作中,您可以将它写成:

if @page.blank?
  flash[:error] = 'Page not found.'
  redirect_to root_path
end

根据控制器代码,请修改案例块。

def main
    #@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

    page_id = (I18n.locale.to_s == 'ru') ? 4 : 6
    @page = Page.where(id: page_id).first  
    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