ruby on rails - Contact Form (SimpleForm) - Route not found/unable to autoload? -
i'm trying install contact page on ruby on rails app. seems straight forward enough, after installing mailer gems, , creating controller with:
$ rails generate controller contact_form new create
i navigate contact url (/contact_form/new), , says
"unable autoload constant contactformcontroller, expected /home/ubuntu/workspace/app/controllers/contact_form_controller.rb define it"
routes , controller follows:
routes.rb
get 'contact_form/new' 'contact_form/create' resources :contact_forms
contact_form_controller.rb
class contactformscontroller < applicationcontroller def new @contact_form = contactform.new end def create begin @contact_form = contactform.new(params[:contact_form]) @contact_form.request = request if @contact_form.deliver flash.now[:notice] = 'thank message!' else render :new end rescue scripterror flash[:error] = 'sorry, message appears spam , not delivered.' end end end
contact_form.rb
class contactform < mailform::base attribute :name, :validate => true attribute :email, :validate => /\a([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i attribute :message attribute :nickname, :captcha => true # declare e-mail headers. accepts mail method # in actionmailer accepts. def headers { :subject => "my contact form", :to => "your_email@example.org", :from => %("#{name}" <#{email}>) } end end
note class named contactformscontroller
, rails looking contactformcontroller
. need pay careful attention pluralization in rails.
- controllers plural.
- models singular.
- routes plural in cases.
- the pluralization of classes must match file name.
so why rails looking contactformcontroller
? because routes not defined properly:
get 'contact_form/new' 'contact_form/create'
get 'contact_forms/new'
proper route form create new resource. don't create resources get rid of get 'contact_form/create'
.
resources :contact_forms
is all need.
so fix error should:
- rename
contact_form_controller.rb
->contact_forms_controller.rb
. - change route definition.
- request
/contact_forms/new
instead.
Comments
Post a Comment