ruby on rails - No Method Error in show.html.erb, known good works fine, file copy/paste doesn't work -


i'm running rails 4./ruby 2.3 environment trying rails app , working , it's breaking somewhere down line following error:

nomethoderror in invoices#show
undefined method `company' nil:nilclass

    <p>       <strong>company:</strong>       <%= @invoice.company %>     </p>      <p> 

now, i've added alot of styling , edits app build functionality , appearance. may have broken , not sure where. did fix make app same "rails generate" commands , variables before, it's basic app. work.

looking @ 2 app's side side, within invoicescontroller, same:

# /invoices/1 # /invoices/1.json def show end 

when copy show.html.erb file known , tested app, following:

nomethoderror in invoices#show undefined method `amount' nil:nilclass

<p> <strong>amount:</strong> <%= @invoice.amount %> <%= @invoice.currency %>  </p> 

i looked @ error terminal , got this:

activerecord::schemamigration load (0.8ms)  select "schema_migrations".*      "schema_migrations" processing invoicescontroller#show html parameters: {"id"=>"14"} rendered invoices/show.html.erb within layouts/application 35ms)         completed 500 internal server error in 68ms (activerecord: 0.0ms)  app/views/invoices/show.html.erb:12:in `_app_views_invoices_show_html_erb__4598034618316500664_30241380' 

here top of invoicescontroller:

class invoicescontroller < applicationcontroller  before_action :set_invoice, only: [:show, :edit, :update, :destroy] 

can help? ideas?

the problem you've not populated @invoice (nil:nilclass), hence cannot call .company on it.

you should have following:

#app/controllers/invoices_controller.rb class invoicescontroller < applicationcontroller    before_action :set_invoice, only: [:show, :edit, :update, :destroy]     def show    end     private     def set_invoice       @invoice = invoice.find params[:id]    end end  #app/views/invoices/show.html.erb <%= @invoice.company %> 

nomethoderror in invoices#show undefined method `company' nil:nilclass

the best way work around (not fix) use .try method:

<%= @invoice.try :company %> 

just fyi in case hit similar problem in future.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -