1

I am trying to show a form on the contact page, but it is from another controller.

The current code results in "First argument in form cannot contain nil or be empty"

After searching it looks there is a problem with the local hash that is not passed.

How can I correctly pass the locals with this code so that it works?

inquiries_controller.rb

class InquiriesController < ApplicationController

 def new
    @inquiry = Inquiry.new
 end
 def create
    @inquiry = Inquiry.new(params[:inquiry])
       if @inquiry.deliver
          render :thank_you
       else
          render :new
    end
  end
end

inquiries_form.html.erb

<%= form_for @inquiry do |f|
       f.text_field :name
       f.text_field :email
       f.text_area :message
       f.submit "Send"
 end %>

static_pages\contact.html.erb

<%= render "inquiries/form",
           :inquiry => @inquiry %>
1
  • I think the problem is that :inquiry => @inquiry sets a local variable (no @) named inquiry, whereas your form expects an instance variable (@). Commented Oct 2, 2014 at 11:06

1 Answer 1

2

HI try adding this to your StaticPages Controller

   class StaticPagesController < ApplicationController
     def contact
      @inquiry = Inquiry.new
    end
   end

Its a very common mistake. Also I believe your form may also be wrong unless you are using a gem that allows for that type of form. Let me know if this will fix your error.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked. Not sure if this is phrased correctly but ... Is it standard to define the variables in the action on the controller which the partial is rendered rather than the controller from which the partial comes?
yes!! :) remember understanding the MVC pattern is really important. If you have two different controllers one home controller, and one post controller and you want to be able to render forms or actions from the post controller to the home controller you have to edit the controller by adding @post = Post.new to the home controller to be able to perform the actions of the post MVC to the home page. I hope this help. Then you can <%= render 'posts/form %> to the home page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.