Rails - 控制器变量在仅限生产的视图中为零。在当地工作正常

时间:2016-03-04 15:01:38

标签: ruby-on-rails production-environment

尝试导航至domain.com/company_entites/new。它会点击new操作并根据new视图呈现new.html.erb表单。这在当地工作正常。

但是在服务器上 - 同样的序列错误了!

通过调试,我们看到 - 对于CompanyEntitiesController - 没有任何操作将数据集传递给@variables。但无法理解为什么?

我们更改了变量的名称。我们使用常量(而不是来自DB)设置变量,但变量在视图中显示nil。注意 - 这仅在生产服务器中发生,但相同的代码在本地工作正常!

CompanyEntitesController.rb

class CompanyEntitiesController < ApplicationController
  before_filter :authenticate_user!
  before_action :set_company_entity, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def new
   @company_entity = CompanyEntity.new
   respond_with(@company_entity)
  end
end

错误

Started GET "/company_entities/new" for 1.1.1.1 at 2016-03-04 09:39:57 -0500 I, 

[2016-03-04T09:39:57.737434 #7243]  INFO -- : Processing by CompanyEntitiesController#new as HTML I,

[2016-03-04T09:39:57.826523 #7243]  INFO -- :   Rendered company_entities/_form.html.erb (5.7ms) I,

[2016-03-04T09:39:57.826932 #7243]  INFO -- :   Rendered company_entities/new.html.erb within layouts/admin_lte_2 (11.6ms) E,

[2016-03-04T09:39:57.827367 #7243] ERROR -- : Caught exception : ArgumentError : First argument in form cannot contain nil or be empty E,


[2016-03-04T09:39:57.827485 #7243] ERROR -- : /home/bobdiya/apps/2dotO_staging/shared/bundle/ruby/2.0.0/gems/actionview-4.1.12/lib/action_view/helpers/form_helper.rb:423:in `form_for'
/home/bobdiya/apps/2dotO_staging/releases/20160304142845/app/views/company_entities/_form.html.erb:3:in `_app_views_company_entities__form_html_erb__3158067205622819281_41143500'
/home/bobdiya/apps/2dotO_staging/shared/bundle/ruby/2.0.0/gems/actionview-4.1.12/lib/action_view/template.rb:145:in `block in render'
/home/bobdiya/apps/2dotO_staging/shared/bundle/ruby/2.0.0/gems/activesupport-4.1.12/lib/active_support/notifications.rb:161:in `instrument'
/home/bobdiya/apps/2dotO_staging/shared/bundle/ruby/2.0.0/gems/actionview-4.1.12/lib/action_view/template.rb:339:in `instrument'
I, 

[2016-03-04T09:39:57.829225 #7243]  INFO -- :   Rendered text template (0.1ms)
I, 
[2016-03-04T09:39:57.829886 #7243]  INFO -- : Completed 500 Internal Server Error in 92ms (Views: 1.4ms)

修改

  • 调整为@brahmana的建议

 def new
    @company_entity = CompanyEntity.new
    Rails.logger.debug @company_entity.inspect+"debugging company----------------------------------------"
 end

/app/views/company_entities/new.html.erb

<section class="content">
<div class="box box-primary">
 <div class="box-header">
   <h3 class="box-title">New Company Entity</h3>
 </div><!-- /.box-header -->

<%= render 'form' %>

/app/views/company_entities/_form.html.erb

   <div class="box-body">
  <%= debug @company_entity %>
 <%= debug "debugging in view ========================== "%>
  <%= form_for(@company_entity) do |f| %>
   <% if @company_entity.errors.any? %>
     <div id="error_explanation">
       <h2><%= pluralize(@company_entity.errors.count, "error") %> prohibited this company_entity from being saved:</h2>

      <ul>
      <% @company_entity.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.label :area %><br>
    <%= f.text_field :area , class: "form-control"%>
  </div>
  <div class="form-group">
    <%= f.label :address %><br>
    <%= f.text_field :address , class: "form-control"%>
  </div>
  <div class="form-group">
    <%= f.label :code %><br>
    <%= f.text_field :code , class: "form-control"%>
  </div>
   <div class="form-group">
    <%= f.label :ext %><br>
    <%= f.text_field :ext , class: "form-control"%>
  </div>
  <div class="form-group">
    <%= f.label :type %><br>
    <%= f.collection_select(:type, CompanyEntity.space_types, :first, :last, {prompt: "Select a category"}, {class: "form-control"}) %>
  </div>
  <div class="form-group">
    <%= f.label :city_id %><br>
    <%= f.collection_select(:city_id, City.all, :id, :name, {prompt: "Select a City"}, {class: "form-control"}) %>
  </div>
 </div>  
<div class="box-footer" >
    <%= f.submit "Save", class: "btn btn-primary " %>
  </div>
<% end %>

在日志中调试跟踪

    Started GET "/company_entities/new" for 106.51.225.130 at 2016-03-07 01:41:37 -0500 I,
    [2016-03-07T01:41:37.643152 #19039]  INFO -- : Processing by CompanyEntitiesController#new as HTML I,
    [2016-03-07T01:41:37.668663 #19039]  INFO -- :   Rendered company_entities/_form.html.erb (11.4ms) I,
    [2016-03-07T01:41:37.668962 #19039]  INFO -- :   Rendered company_entities/new.html.erb within layouts/admin_lte_2 (14.7ms)E,
    [2016-03-07T01:41:37.669333 #19039] ERROR -- : Caught exception : ArgumentError : First argument in form cannot contain nil or be empty E,
    [2016-03-07T01:41:37.669407 #19039] ERROR -- : /home/bobdiya/apps/2dotO_staging/shared/bundle/ruby/2.0.0/gems/actionview-4.1.12/lib/action_view/helpers/form_helper.rb:423:in `form_for'
/home/bobdiya/apps/2dotO_staging/releases/20160307064025/app/views/company_entities/_form.html.erb:5:in `_app_views_company_entities__form_html_erb__779177916647948870_62782300'
/home/bobdiya/apps/2dotO_staging/shared/bundle/ruby/2.0.0/gems/actionview-4.1.12/lib/action_view/template.rb:145:in `block in render'
/home/bobdiya/apps/2dotO_staging/shared/bundle/ruby/2.0.0/gems/activesupport-4.1.12/lib/active_support/notifications.rb:161:in `instrument'
/home/bobdiya/apps/2dotO_staging/shared/bundle/ruby/2.0.0/gems/actionview-4.1.12/lib/action_view/template.rb:339:in `instrument'I,


    [2016-03-07T01:41:37.670276 #19039]  INFO -- :   Rendered text template (0.0ms)
I, 
   [2016-03-07T01:41:37.670584 #19039]  INFO -- : Completed 500 Internal Server Error in 27ms (Views: 0.7ms)
I, 
   [2016-03-07T01:41:37.790672 #19036]  INFO -- : Started GET "/favicon.ico" for 106.51.225.130 at 2016-03-07 01:41:37 -0500
I, 
   [2016-03-07T01:41:37.806287 #19036]  INFO -- : Processing by ApplicationController#no_route as 
I, 
   [2016-03-07T01:41:37.806478 #19036]  INFO -- :   Parameters: {"unmatched_route"=>"favicon"}
I, 
   [2016-03-07T01:41:37.817066 #19036]  INFO -- :   Rendered application/no_route.html.erb within layouts/admin_lte_2 (3.3ms)
I, 


[2016-03-07T01:41:38.947676 #19036]  INFO -- :   Rendered layouts/_notification.html.erb (5.9ms)
I, [2016-03-07T01:41:38.949171 #19036]  INFO -- :   Rendered layouts/_admin_lte_2_header.html.erb (11.9ms)
I, [2016-03-07T01:41:38.962706 #19036]  INFO -- :   Rendered layouts/_admin_lte_2_sidebar.html.erb (10.3ms)
I, [2016-03-07T01:41:38.973312 #19036]  INFO -- :   Rendered popups/_add_new_contact.html.erb (7.6ms)
I, [2016-03-07T01:41:38.974101 #19036]  INFO -- : Completed 200 OK in 1167ms (Views: 1166.7ms)

注意:我已将new重命名为new1并更改了路由首选项,并在控制器和视图中使用相同的代码 - 它可以正常工作!

0 个答案:

没有答案