Search code examples
ruby-on-railsrubyrenderpartial

Render partial in different controller


I have a Orders controller and in my New.html.haml controller view I'm trying to render the _form partial for address

order new.html.haml

= render "/addresses/form"

address _form.html.haml

= form_for @address do |f|
  - if @address.errors.any?
  #error_explanation
    %h2= "#{pluralize(@address.errors.count, "error")} prohibited this address from being saved:"
    %ul
      - @address.errors.full_messages.each do |msg|
        %li= msg

  .field

   # All the form fields

  .actions
    = f.submit 'Save'

I've been getting the error

First argument in form cannot contain nil or be empty

What would be the best way to fix this? I though of creating a partial just for that but I don't think creating another form partial would be in compliance to DRY.

Thanks a lot in advance


Solution

  • The reason for this error is that @address is nil To fix you need to instantiate in the controller

    # orders_controller.rb
    def new
      @address = Address.new
    end
    
    def edit
      @address = Address.find params[:id]
    end