I am working through Daniel Kehoe's tutorial on rails and have run into a problem that I do not understand and cannot find an answer to online. I get the NoMethodError described in the title of this post when using a flash message with params.
Here is the controller
class ContactsController < ApplicationController
def process_form
Rails.logger.debug "DEBUG: params are #{params.inspect}"
flash[:notice] = "Received request from #{params[:contact][:name]}"
redirect_to root_path
end
end
It works if I remove [:contact] and leave [:name]. If I remove [:name] and leave [:contact] it redirects but doesn't display the name. If I have both [:contact] and [:name] as the controller listing shows I get the NoMethodError and it does not redirect.
Here is my view page
<% content_for :title do %>Contact<% end %>
<h3>Contact</h3>
<div class="form">
<%= form_with( url: contact_path) do |form| %>
<%= form.label :name %>
<%= form.text_field :name, autofocus: true %>
<br/>
<br/>
<%= form.label :email %>
<%= form.email_field :email %>
<br/>
<br/>
<%= form.label 'message' %>
<%= form.text_area :content, size: '40x5' %>
<br/>
<br/>
<%= form.submit 'Submit', class: 'submit' %>
<% end %>
</div>
Here is my routes.rb
Rails.application.routes.draw do
post 'contact', to: 'contacts#process_form'
root to: 'visitors#new'
end
Here is my log when I get the NoMethodError:
Started POST "/contact" 2017-07-23 23:25:11 +0000 Processing by ContactsController#process_form as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "name"=>"Jane Doe", "email"=>"[email protected]", "content"=>"oiuwroiueroieroiuerw", "commit"=>"Submit"} DEBUG: params are "✓", "authenticity_token"=>"...", "name"=>"Jane Doe", "email"=>"[email protected]", "content"=>"oiuwroiueroieroiuerw", "commit"=>"Submit", "controller"=>"contacts", "action"=>"process_form"} permitted: false> Completed 500 Internal Server Error in 3ms
NoMethodError (undefined method `[]' for nil:NilClass):