I will setup the scenario followed by my question, in detail.
Here is the scenario:
I have a contact controller, with new and create actions. This is for a form I'd like users to fill out and to store their responses.
Contact controller:
class ContactController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
respond_to do |format|
if @contact.save
format.html { redirect_to social_path, notice: 'Message sent successfully!' }
else
format.html { render action: 'new' }
end
end
end
end
I have a corresponding _form.html.erb partial, as well as a new.html.erb view, which im rendering the form partial, as you would do. These views are all located inside of the contact view folder, with corresponding routes.
_form.html.erb
<%= form_for @contact do |f| %>
<% if @contact.errors.any? %>
<h2>
<%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved:
</h2>
<ul>
<% @contact.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div class="row collapse">
<div class="large-2 columns">
<%= f.label :name, :class => 'inline' %>
</div>
<div class="large-10 columns">
<%= f.text_field :name, :placeholder => 'Kenny Powers' %>
</div>
</div>
<div class="row collapse">
<div class="large-2 columns">
<%= f.label :subject %>
</div>
<div class="large-10 columns">
<%= f.text_field :subject %>
</div>
</div>
<div class="row collapse">
<div class="large-2 columns">
<%= f.label :email %>
</div>
<div class="large-10 columns">
<%= f.text_field :email, :placeholder => 'kennypowers@example.com' %>
</div>
</div>
<div class="row collapse">
<div class="large-2 columns">
<%= f.label :message %>
</div>
<div class="large-10 columns">
<%= f.text_area :message %>
</div>
</div>
<div class="row collapse">
<div class="large-2 columns end">
<%= f.submit %>
</div>
</div>
<% end %>
new.html.erb <%= render 'contact/form' %>
I have created a static_pages controller, as I wanted to keep my mostly static pages separate from pages loading dynamic content.
Inside of the static_pages controller I have a social controller action which is empty. I'm displaying static content at social.html.erb, which is located in the static_pages view folder, with a match route as follows
match 'social' => 'static_pages#social'
social.html.erb
<%= render :template => 'contact/new', :@contact => Contact.new %>
static_pages_controller
def social
end
Cool, the social pages renders perfectly.
Now for the part im confused with:
How do I render the contact form in the social page? As it sits in the social.html.erb file I have
<%= render template: 'contact/new' %>
which gives me
ActionView::MissingTemplate in Static_pages#social
Showing app/views/contact/new.html.erb where line #1 raised:
1: <%= render 'form' %>
It is throwing a missing template error for the _form.html.erb in the contact/new.html.erb. If I try to specify the form location by adding
<%= render 'contact/form' %>
I get a undefined method model_name for NilClass:Class
error.
Am I doing this completely wrong? Is there a better way to do what I am attempting? Can someone provide me with a layman explanation?
I know there are similar questions with rendering form partials into other controllers, but they are only one line solutions. I would greatly appreciate words to support WHY this is happening, or words to support a better/correct way of doing things.
If you need any more information/specific code, please let me know.