Máté Kaszás
2013-Jun-23 04:20 UTC
Contact form - NoMethodError (undefined method `each'' for nil:NilClass)
Hi Guys, So basically I want a contact form to appear on all the service pages. I''ve already created a contact form for the contact page which works perfectly using this tutorial: http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/ Now I would like to implement this approach on the service pages, and it works until I push the Submit button, when I get the following error: Processing by ScontactController#create as HTML Completed 500 Internal Server Error in 1ms NoMethodError (undefined method `each'' for nil:NilClass): app/models/servicemail.rb:13:in `initialize'' app/controllers/scontact_controller.rb:9:in `new'' app/controllers/scontact_controller.rb:9:in `create'' The parameters are ok. Rails seems to complain about the each method in the model: class Servicemail include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor :name, :email, :subject, :body, :szolg validates :name, :email, :subject, :body, :presence => true validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true def initialize(attributes = {}) attributes.each do |name, value| send("#{name}=", value) end end def persisted? false end end This is the controller: class ScontactController < ApplicationController def new @message = Servicemail.new @szolg = Service.find_by_permalink!(params[:id]) end def create @message = Servicemail.new(params[:message]) @szolg = Service.find_by_permalink(params[:id]) if @message.valid? ServiceContact.new_message(@message).deliver redirect_to(szolg_path(@szolg), :notice => "Message sent successfully!") else flash.now.alert = "Please fill out all the required fields!" render :new end end end Is it the name of the model/view/controller that bothers Rails or is it something else? Best Wishes, Matt -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7b49e328-2ca1-4f10-bfd7-c46becc3f743%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Jun-23 16:23 UTC
Re: Contact form - NoMethodError (undefined method `each'' for nil:NilClass)
On 23 June 2013 05:20, Máté Kaszás <kaszas.mate-PWztcFTdOMWHXe+LvDLADg@public.gmane.org> wrote:> Hi Guys, > > So basically I want a contact form to appear on all the service pages. > I''ve already created a contact form for the contact page which works > perfectly using this tutorial: > http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/ > > Now I would like to implement this approach on the service pages, and it > works until I push the Submit button, when I get the following error: > > Processing by ScontactController#create as HTML > > Completed 500 Internal Server Error in 1ms > > NoMethodError (undefined method `each'' for nil:NilClass): > app/models/servicemail.rb:13:in `initialize''That means that at that line of code you are calling ''each'' on something that is nil.> app/controllers/scontact_controller.rb:9:in `new'' > app/controllers/scontact_controller.rb:9:in `create'' > > The parameters are ok. > > Rails seems to complain about the each method in the model: > class Servicemail > > include ActiveModel::Validations > include ActiveModel::Conversion > extend ActiveModel::Naming > > attr_accessor :name, :email, :subject, :body, :szolg > > validates :name, :email, :subject, :body, :presence => true > validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => > true > > def initialize(attributes = {}) > attributes.each do |name, value|Assuming that the above is line 13 then it means that attributes is nil. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsdfNpm3rgWboLsAfeugh5cmk71SntH0yhVhM1g%3DQ2E5g%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Possibly Parallel Threads
- keep getting "undefined method `map' for nil:NilClass (NoMethodError) " in new server
- has_one :through NoMethodError: undefined method `klass' for nil:NilClass when doing class_name on reflection
- NoMethodError (undefined method `fetch_fields' for nil:NilClass)
- Bug: NoMethodError: private method `gsub'' called for nil:NilClass
- NoMethodError: undefined method `accept' for nil:NilClass