简体   繁体   中英

undefined method `errors' for nil:NilClass

Here is my Edited details:

I have my controller like,

class Enr::Rds::SurvRdsapXrefsController < Enr::Controller

  def index
    @enr_rds_surv_rdsap_xrefs = Enr::Rds::SurvRdsapXref.paginate(page: params[:page])
  end

  def show
  end

  def new
     @enr_rds_surv_rdsap_xref = Enr::Rds::SurvRdsapXref.new
  end

  def edit
    @enr_rds_surv_rdsap_xref = Enr::Rds::SurvRdsapXref.find(params[:id])
  end

  def create
    @enr_rds_surv_rdsap_xref = Enr::Rds::SurvRdsapXref.new(params[:enr_rds_surv_rdsap_xref])

    respond_to do |format|
      if @enr_rds_surv_rdsap_xref.save
        format.html { redirect_to :enr_rds_surv_rdsap_xrefs, notice: "Survey link was successfully created." }
        format.js
        format.json { render json: @enr_rds_surv_rdsap_xref, status: :created, location: @enr_rds_surv_rdsap_xref }
      else
        format.html { render action: "new" }
        format.js
        format.json { render json: @enr_rds_surv_rdsap_xref.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
  end

  def destroy
  end
end

Here is my view form like

<%= form_for(@enr_rds_surv_rdsap_xref, :remote => true) do |f| %>

<% if @enr_rds_surv_rdsap_xref.errors.any? %>
  <div id="error_explanation">
    <div class="validate">
      The form contains <%= pluralize@enr_rds_surv_rdsap_xref.errors.count, "error") %>.
    </div>
    <ul>
    <% @enr_rds_surv_rdsap_xref.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

<div class="control-group">
  <%= f.label :section %><br />
  <%= f.text_field :section %>
</div>

<%= f.submit %>

<% end %>

When i click the index page to create a new link, the index page showing error like

NoMethodError in Enr/rds/surv_rdsap_xrefs#index

undefined method `errors' for nil:NilClass

Thanks for the suppport and please suggest me to rectify the error. I am new to ROR. Thanks

Your error reveals that the rendering of the index template is causing the error, which means you're rendering the form for the new survey (the code snippet above) in the index template. This is fine, but if you're going to do that, you'll have to instantiate a new survey in index , as well as in new .

At the simplest, you could just copy the code in new to index :

def index
  @enr_rds_surv_rdsap_xrefs = Enr::Rds::SurvRdsapXref.paginate(page: params[:page])
  @enr_rds_surv_rdsap_xref = Enr::Rds::SurvRdsapXref.new
end

def new
   @enr_rds_surv_rdsap_xref = Enr::Rds::SurvRdsapXref.new
end

To keep your code a bit DRYer you might change where the new instance is created. A pattern you'll often see is something similar to:

before_filter :build_record, :only => [:new, :index]

protected

def build_record
  @survey = YourSurvey.new
end

This way you don't even need to write the new/index methods if you don't have any other logic.

Do you also set @survey in the new action in your controller? The error means that when the view is rendered @survey is nil, so there must be a problem with setting that instance variable.

Do you get the error when you go to the 'new' view or when you try to submit the form (create)?

The form contains <%= pluralize@enr_rds_surv_rdsap_xref.errors.count, "error") %>

This line of the code is the problem. You are lacking a "(" between the pluralize and the @enr...

Explained: RoR thinks that the object is: pluralize@enr... instead of the @ All alone, and he has no errors for this kind of object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM