2012-12-20 33 views
3

我在我的應用程序中使用'refinerycms-queries'gem在我的聯繫頁面上顯示查詢。煉油廠CMS查詢 - content_for渲染時出現NoMethodError:正文內容

我得到的問題是,儘管我可以在我的其他頁面上使用<%= raw @page.content_for(:body) %>代碼來呈現內容,但我不知道爲什麼我在查詢#新視圖中獲得此代碼。也許有人可以幫助我更快地解決這個問題,因爲我已經在互聯網上找了幾個小時尋找答案並查看源代碼。

這是錯誤:

NoMethodError in Refinery/inquiries/inquiries#new 
undefined method `content_for' for nil:NilClass 

Extracted source (around line #3): 

1: <% content_for :body do %> 
2:  <div> 
3:  <%= raw @page.content_for(:body) %> 
4:  </div> 
5: <% end %> 
6: 

我不得不提到我是一個有點新的煉油廠和回報率。

這是我的煉油廠/查詢/查詢/ new.html.erb文件:

<% content_for :body do %> 
    <div class="contact"> 
     <%= raw @page.content_for(:body) %> 
    </div> 
<% end %> 

<% content_for :body_content_left do %> 
    <div class='inquiries'> 
     <%= form_for [refinery, :inquiries, @inquiry] do |f| %> 
      <%= render :partial => "/refinery/admin/error_messages", 
        :locals => { 
          :object => @inquiry, 
          :include_object_name => true 
        } %> 
      <div class="field"> 
      <%= f.required_label :name, :class => 'placeholder-fallback' %> 
      <%= f.text_field :name, :class => 'text', :required => 'required', :placeholder => t('name', :scope => 'activerecord.attributes.refinery/inquiries/inquiry') %> 
      </div> 
      <div class="field"> 
      <%= f.required_label :email, :class => 'placeholder-fallback' %> 
      <%= f.email_field :email, :class => 'text email', :required => 'required', :placeholder => t('email', :scope => 'activerecord.attributes.refinery/inquiries/inquiry') %> 
      </div> 
      <div class="field"> 
      <%= f.label :phone, :class => 'placeholder-fallback' %> 
      <%= f.text_field :phone, :class => 'text phone', :placeholder => t('phone', :scope => 'activerecord.attributes.refinery/inquiries/inquiry') %> 
      </div> 
      <div class='field message_field'> 
      <%= f.required_label :message, :class => 'placeholder-fallback' %> 
      <%= f.text_area :message, :rows => 8, :required => 'required', :placeholder => t('message', :scope => 'activerecord.attributes.refinery/inquiries/inquiry') %> 
      </div> 
      <div class="actions"> 
      <%= f.submit t('.send') %> 
      </div> 
     <% end %> 
    </div> 
<% end %> 
<%= render :partial => "/refinery/content_page" %> 

這是我inquiries_controller.rb:

module Refinery 
    module Inquiries 
    class InquiriesController < ::ApplicationController 

     before_filter :find_page, :only => [:create, :new] 

     def thank_you 
     @page = ::Refinery::Page.find_by_link_url("/contact/thank_you") 
     end 

     def new 
     @inquiry = ::Refinery::Inquiries::Inquiry.new 
     end 

     def create 
     @inquiry = ::Refinery::Inquiries::Inquiry.new(params[:inquiry]) 

     if @inquiry.save 
      if @inquiry.ham? 
      begin 
       ::Refinery::Inquiries::InquiryMailer.notification(@inquiry, request).deliver 
      rescue 
       logger.warn "There was an error delivering an inquiry notification.\n#{$!}\n" 
      end 

      begin 
       ::Refinery::Inquiries::InquiryMailer.confirmation(@inquiry, request).deliver 
      rescue 
       logger.warn "There was an error delivering an inquiry confirmation:\n#{$!}\n" 
      end if ::Refinery::Inquiries::Setting.send_confirmation? 
      end 

      redirect_to refinery.thank_you_inquiries_inquiries_path 
     else 
      render :action => 'new' 
     end 
     end 

     protected 

     def find_page 
     @page = ::Refinery::Page.find_by_link_url("/contact") 
     end 

    end 
    end 
end 

這裏是配置/ routes.rb中部分是由'refinerycms-queries'創建的寶石:

Refinery::Core::Engine.routes.draw do 
    namespace :inquiries, :path => '' do 
     get '/contact', :to => 'inquiries#new', :as => 'new_inquiry' 

     resources :contact, 
       :only => :create, 
       :as => :inquiries, 
       :controller => 'inquiries' do 
     get :thank_you, :on => :collection 
     end 

     namespace :admin, :path => 'refinery' do 
     resources :inquiries, :only => [:index, :show, :destroy] do 
      get :spam, :on => :collection 
      get :toggle_spam, :on => :member 
     end 

     scope :path => 'inquiries' do 
      resources :settings, :only => [:edit, :update] 
     end 
     end 
    end 
    end 
+0

這是一個有趣的問題。我有類似的問題,因爲我試圖調試基本上使自定義的查詢引擎的表單生成器 – CJBrew

回答

2

看起來像我已經找到了問題。

原來,從inquiries_controller ::煉油廠:: Page.find_by_url方法返回零,我不得不重寫find_page和thank_you方法是這樣的:

def thank_you 
    @page = ::Refinery::Page.find_by_path("/contact/thank_you") 
    end 

    def find_page 
    @page = ::Refinery::Page.find_by_path(/contact") 
    end 

爲了使:身體: side_body內容將在我的聯繫人視圖中呈現。我也注意到,通過這種修改,並不需要在我的煉油廠/ queries/queries/new.html.erb文件中明確指定呈現content_for(:body)。

所以新的觀點看起來像一個默認的寶石:

<% content_for :body_content_left do %> 
    <div class='inquiries'> 
     <%= form_for [refinery, :inquiries, @inquiry] do |f| %> 
      <%= render :partial => "/refinery/admin/error_messages", 
        :locals => { 
          :object => @inquiry, 
          :include_object_name => true 
        } %> 
      <div class="field"> 
      <%= f.required_label :name, :class => 'placeholder-fallback' %> 
      <%= f.text_field :name, :class => 'text', :required => 'required', :placeholder => t('name', :scope => 'activerecord.attributes.refinery/inquiries/inquiry') %> 
      </div> 
      <div class="field"> 
      <%= f.required_label :email, :class => 'placeholder-fallback' %> 
      <%= f.email_field :email, :class => 'text email', :required => 'required', :placeholder => t('email', :scope => 'activerecord.attributes.refinery/inquiries/inquiry') %> 
      </div> 
      <div class="field"> 
      <%= f.label :phone, :class => 'placeholder-fallback' %> 
      <%= f.text_field :phone, :class => 'text phone', :placeholder => t('phone', :scope => 'activerecord.attributes.refinery/inquiries/inquiry') %> 
      </div> 
      <div class='field message_field'> 
      <%= f.required_label :message, :class => 'placeholder-fallback' %> 
      <%= f.text_area :message, :rows => 8, :required => 'required', :placeholder => t('message', :scope => 'activerecord.attributes.refinery/inquiries/inquiry') %> 
      </div> 
      <div class="actions"> 
      <%= f.submit t('.send') %> 
      </div> 
     <% end %> 
    </div> 
<% end %> 
<%= render :partial => "/refinery/content_page" %> 

希望這有助於如果有人遇到同樣的問題。

+0

我想你已經在'_new'表單上做了重載,但是你可以與我們分享如何修改控制器的查詢(假設對Refinery控制器的任何修改都是由裝飾器完成的) ? –

+0

是的,您是對的,我可能在那裏犯了一個大錯,因爲我已經在我的煉油廠應用程序中覆蓋了控制器,這就是它目前的工作方式。畢竟,這不是最好的解決方案,但它是一種解決方法(我仍在學習:-)) – ma3x

+0

你沒錯,你很勇敢。繼續,並有一個很好的編碼日子。 –

0

問題是您的相關文件缺少@page的定義自動控制器的行爲(在你的案例中「新」和「創造」)。我可以看到@inquiry實例化。嘗試添加語句

@page = ::Refinery::Page.find_by_link_url("/contact") 

無論你在'新'和'創建'的InquriesController行動。

我有點好奇,你使用的是什麼版本的煉油廠? (奇怪的是refinerycms-queries gem如何在routes.rb中生成一個內容,假設它是一個可掛載的引擎)。

+0

實際上有一個before_filter:find_page爲新的和創建操作。這是正確的在控制器寫的頂部我使用的煉油廠版本是2.0.8 – ma3x

+0

你是絕對正確的,變量@page被設置。讓我們試着去調試它:cd到你的項目的目錄並輸入'rails c'然後':: Refinery :: Page.find_by_link_url(「/ contact」)'。輸出是什麼? –

+0

畢竟我設法解決了這個問題,但自從我發佈我的問題以來,我無法發佈我的答案,因爲我沒有足夠的聲望。問題是在db中沒有頁面有link_url,所以我使用了find_by_path方法,它返回我請求的精煉頁面。當我在rails控制檯中寫入時,它返回nil。當我被允許與控制器和視圖中的所有修改後的代碼一起被允許時,我會發布答案對不起,我在寫完所有內容之前意外打開了輸入:) – ma3x