3

我在我的应用程序中使用“refinerycms-inquiries”gem 在我的联系页面上进行查询。

我遇到的问题是,虽然我可以使用<%= raw @page.content_for(:body) %>其他页面上的代码来呈现内容,但我不知道为什么我会在查询#new 视图中得到这个。也许有人可以帮助我更快地解决这个问题,因为我已经在互联网上寻找了几个小时的答案并查看了源代码。

这是错误:

 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: 

我不得不提到我对炼油厂和 RoR 有点陌生。

这是我的炼油厂/inquiries/inquiries/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

这是由“refinerycms-inquiries”gem 生成的 config/routes.rb 部分:

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
4

2 回答 2

2

看来我找到了问题所在。

事实证明,inquiries_controller 中的 ::Refinery::Page.find_by_url 方法返回 nil,我不得不像这样重写 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

为了在我的联系人视图中呈现 :body 和 :side_body 内容。我还注意到,通过这种修改,不需要在我的炼油厂/查询/查询/new.html.erb 文件中明确指定来呈现 content_for(:body)。

所以新视图看起来就像 gem 中的默认视图:

<% 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" %>

如果有人遇到同样的问题,希望这会有所帮助。

于 2012-12-20T23:20:25.510 回答
0

问题是在您的相应控制器的操作中缺少@page 的定义(在您的情况下为“新建”和“创建”)。我可以看到 @inquiry 实例化。尝试添加语句

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

在 InquriesController 的“新建”和“创建”操作中。

我有点好奇,你用的是什么版本的炼油厂?(奇怪的是,refinerycms-inquiries gem 如何在 routes.rb 中生成内容,假设它是一个可安装的引擎)。

于 2012-12-20T16:01:41.493 回答