Reputation: 429
I'm rails noob. I have a problem with contact form.
I have an error: undefined method 'name' for nil:NilClass
<p>
<strong>Name:</strong>
<%= @contact_forms.name %>
</p>
My manager.html.erb is:
<div>
<p>
<strong>Name:</strong>
<%= @contact_forms.name %>
</p>
<p>
<strong>Text:</strong>
<%= @contact_forms.text %>
</p>
</div>
My contact_form_controller.rb is:
class ContactFormController < ApplicationController
def new
end
def create
@contact_forms = Contact_form.new(params[:contact_form])
@contact_forms.save
redirect_to root_path
end
def show
@contact_forms = Contact_form.all
end
end
My file with migrates:
class CreateContactForms < ActiveRecord::Migration
def change
create_table :contact_forms do |t|
t.string :name
t.string :phone
t.string :email
t.text :text
t.timestamps null: false
end
end
end
my static_pages_controller.rb is
class StaticPagesController < ApplicationController
def home
end
def manager
end
end
Thanks
Upvotes: 1
Views: 62
Reputation: 76774
I'm rails noob
Welcome to the family! You're a newb, not noob :)
Pavan's answer is right; since you're new I wanted to give you some context:
undefined method 'name' for nil:NilClass
This error means you're trying to call a method on a variable which has not been defined / populated.
In your case, @contact_forms
is not defined.
The confusing part for many new Ruby developers is that instead of halting the entire program, Ruby populates the NilClass
& claims there to be an error with it
So whilst you'd expect it to say the variable was undeclared, it gives you a message about how a method doesn't work.
--
To resolve your issue, you need to use the following:
#config/routes.rb
resources :contact_forms
resources :static_pages
#app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
def manager
@contact_forms = ContactForm.all
end
end
To steal from Pavan, this has to be backed up by looping through the @contact_forms
variable (unless you've populated it with a single instance of the ContactForm
model):
#app/views/static_pages/manager.html.erb
<% @contact_forms.each do |form| %>
<%= form.name %>
<% end %>
As an aside, I would never recommend calling a controller StaticPages
.
When you get more into Ruby, you'll find out about the object orientated nature of the language:
I explain this a lot; basically, it means you have to keep your program centered around objects, in the case of Rails, are populated by the Models
.
As such, you need to think about what data object you're trying to manipulate when showing this view. At present, it seems you want to show a contact
form -- I'd put it in its own method in the ApplicationController
:
#config/routes.rb
match "contact", to: "application#contact_form", via: [:get, :post]
#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def contact_form
if request.post?
#post contact form
else
#load contact form
end
end
end
Upvotes: 1
Reputation: 33542
undefined method `name' for nil:NilClass
You didn't defined @contact_forms
in the manager
method of static_pages
controller, so is the error. Defining it like below should solve your problem.
class StaticPagesController < ApplicationController
def home
end
def manager
@contact_forms = ContactForm.all
end
end
Update:
You should also iterate over @contact_forms
like below in the manager.html.erb
<div>
<% @contact_forms.each do |contact_form| %>
<p>
<strong>Name:</strong>
<%= contact_form.name %>
</p>
<p>
<strong>Text:</strong>
<%= contact_form.text %>
</p>
<% end %>
</div>
Upvotes: 1