Ruby 【求助】刚学习 Ruby,想要开发一个填写联系表格发送邮件的网页

Xenon1997 · 2021年02月03日 · 最后由 aline57963 回复于 2021年02月04日 · 440 次阅读

我正在仿照以下两个方法学习制作一个网站。目的是将网页表单内容 l 发送至指定的邮箱。 http://l4u.github.io/articles/create-a-rails-4-site-with-contact-us-form/#add-contact-us-form https://bugjia.net/200207/67730.html

我根据第一个链接的第五步,编写 Contact.rb,并运行服务器查看效果。此时我能够访问到我需求的表单。

点击 submit 按钮后,rails 提示 NameError in ContactsController#create. 及 Uninitialized constant Mail_Form. 出错位置正是 Contact.rb 的第一行。Contact 无法继承 MailForm。

Stackoverflow 上的相关问题提示,是否未安装 mail_form 的 gem 包。随即使用 gem install mail_form 安装。并运行 bundle install。

运行后再次启动服务器,问题没有解决。报错同上。

利用 gem info 查看,mail_form 的确已经被安装了。

mail_form (1.8.1) Authors: José Valim, Carlos Antônio Homepage: https://github.com/plataformatec/mail_form License: MIT Installed at: C:/Ruby26-x64/lib/ruby/gems/2.6.0

利用 bundle show 指令查看是否 mail_form 被应用。提示 Could not find gem 'mail_form'.

这是网页的布局设计

这是 contacts 的 controller

这是 contacts 的 model

小弟才疏学浅,如有低级问题麻烦指路,感激不尽!

这教程作为入门很不友好,一次过参杂了太多第三方 Gem,guardlivereloadmail_formhaml 这几个 gem 都是很小众的,出问题都不知道是哪一个。

bundle show 的结果贴上来,确认 mail_form 安装了没。

如果已经安装,启动 server 缺提示找不到,有可能是 guard 或者 livereload 导致重启不完整,命令行 ps 一下看看有没有 ruby 进程没退出,啊是 Windows,不懂 Windows 对应的命令是啥。

Rei 回复

非常感谢您的解答。

以下是 bundle show 的结果。 H:\Ruby Program\blog (master) λ bundle show Gems included by the bundle:

  • actioncable (6.0.3.2)
  • actionmailbox (6.0.3.2)
  • actionmailer (6.0.3.2)
  • actionpack (6.0.3.2)
  • actiontext (6.0.3.2)
  • actionview (6.0.3.2)
  • activejob (6.0.3.2)
  • activemodel (6.0.3.2)
  • activerecord (6.0.3.2)
  • activestorage (6.0.3.2)
  • activesupport (6.0.3.2)
  • addressable (2.7.0)
  • bindex (0.8.1)
  • bootsnap (1.4.6)
  • builder (3.2.4)
  • bundler (1.17.2)
  • byebug (11.1.3)
  • capybara (3.33.0)
  • childprocess (3.0.0)
  • concurrent-ruby (1.1.6)
  • crass (1.0.6)
  • erubi (1.9.0)
  • ffi (1.13.1)
  • globalid (0.4.2)
  • i18n (1.8.3)
  • jbuilder (2.10.0)
  • loofah (2.6.0)
  • mail (2.7.1)
  • marcel (0.3.3)
  • method_source (1.0.0)
  • mimemagic (0.3.5)
  • mini_mime (1.0.2)
  • mini_portile2 (2.4.0)
  • minitest (5.14.1)
  • msgpack (1.3.3)
  • nio4r (2.5.2)
  • nokogiri (1.10.9)
  • public_suffix (4.0.5)
  • puma (4.3.5)
  • rack (2.2.3)
  • rack-proxy (0.6.5)
  • rack-test (1.1.0)
  • rails (6.0.3.2)
  • rails-dom-testing (2.0.3)
  • rails-html-sanitizer (1.3.0)
  • railties (6.0.3.2)
  • rake (13.0.1)
  • regexp_parser (1.7.1)
  • rubyzip (2.3.0)
  • sass-rails (6.0.0)
  • sassc (2.4.0)
  • sassc-rails (2.1.2)
  • selenium-webdriver (3.142.7)
  • sprockets (4.0.2)
  • sprockets-rails (3.2.1)
  • sqlite3 (1.4.2)
  • thor (1.0.1)
  • thread_safe (0.3.6)
  • tilt (2.0.10)
  • turbolinks (5.2.1)
  • turbolinks-source (5.2.0)
  • tzinfo (1.2.7)
  • tzinfo-data (1.2020.1)
  • web-console (4.0.3)
  • webdrivers (4.4.1)
  • webpacker (4.2.2)
  • websocket-driver (0.7.2)
  • websocket-extensions (0.1.5)
  • xpath (3.2.0)
  • zeitwerk (2.3.1)

确实,mail_form 没有被安装。 重新运行 gem install 后仍然显示没有安装…这是什么情况?

bundle install 看看输出。

您好 这个是输出。抱歉不知道为什么粘贴格式不太对。已编辑

H:\Ruby Program\blog (master)

λ bundle install

Using rake 13.0.1

Using concurrent-ruby 1.1.6

Using i18n 1.8.3

Using minitest 5.14.1

Using thread_safe 0.3.6

Using tzinfo 1.2.7

Using zeitwerk 2.3.1

Using activesupport 6.0.3.2

Using builder 3.2.4

Using erubi 1.9.0

Using mini_portile2 2.4.0

Using nokogiri 1.10.9 (x64-mingw32)

Using rails-dom-testing 2.0.3

Using crass 1.0.6

Using loofah 2.6.0

Using rails-html-sanitizer 1.3.0

Using actionview 6.0.3.2

Using rack 2.2.3

Using rack-test 1.1.0

Using actionpack 6.0.3.2

Using nio4r 2.5.2

Using websocket-extensions 0.1.5

Using websocket-driver 0.7.2

Using actioncable 6.0.3.2

Using globalid 0.4.2

Using activejob 6.0.3.2

Using activemodel 6.0.3.2

Using activerecord 6.0.3.2

Using mimemagic 0.3.5

Using marcel 0.3.3

Using activestorage 6.0.3.2

Using mini_mime 1.0.2

Using mail 2.7.1

Using actionmailbox 6.0.3.2

Using actionmailer 6.0.3.2

Using actiontext 6.0.3.2

Using public_suffix 4.0.5

Using addressable 2.7.0

Using bindex 0.8.1

Using msgpack 1.3.3 (x64-mingw32)

Using bootsnap 1.4.6

Using bundler 1.17.2

Using byebug 11.1.3

Using regexp_parser 1.7.1

Using xpath 3.2.0

Using capybara 3.33.0

Using childprocess 3.0.0

Using ffi 1.13.1 (x64-mingw32)

Using jbuilder 2.10.0

Using method_source 1.0.0

Using puma 4.3.5

Using rack-proxy 0.6.5

Using thor 1.0.1

Using railties 6.0.3.2

Using sprockets 4.0.2

Using sprockets-rails 3.2.1

Using rails 6.0.3.2

Using rubyzip 2.3.0

Using sassc 2.4.0 (x64-mingw32)

Using tilt 2.0.10

Using sassc-rails 2.1.2

Using sass-rails 6.0.0

Using selenium-webdriver 3.142.7

Using sqlite3 1.4.2

Using turbolinks-source 5.2.0

Using turbolinks 5.2.1

Using tzinfo-data 1.2020.1

Using web-console 4.0.3

Using webdrivers 4.4.1

Using webpacker 4.2.2

Rei 回复

抱歉粘贴分行有些混乱,已经编辑

贴一下 Gemfile,看看有没有 mail_form,另外执行命令的路径有没有弄错。

随即使用 gem install mail_form 安装。并运行 bundle install

楼主是不是跳过了教程的第一步:Edit Gemfile 🤔

Rei 回复

Gemfile:

source 'https://rubygems.org'

git_source(:github) { |repo| "https://github.com/#repo}.git{" }

ruby '2.6.6'

Bundle edge Rails instead: gem 'rails', github: 'rails/rails'

gem 'rails', '~> 6.0.3', '>= 6.0.3.2'

Use sqlite3 as the database for Active Record

gem 'sqlite3', '~> 1.4'

Use Puma as the app server

gem 'puma', '~> 4.1'

Use SCSS for stylesheets

gem 'sass-rails', '>= 6'

Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker

gem 'webpacker', '~> 4.0'

gem 'turbolinks', '~> 5'

Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder

gem 'jbuilder', '~> 2.7'

Use Redis adapter to run Action Cable in production

gem 'redis', '~> 4.0'

Use Active Model has_secure_password

gem 'bcrypt', '~> 3.1.7'

Use Active Storage variant

gem 'image_processing', '~> 1.2'

Reduces boot times through caching; required in config/boot.rb

gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end

group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' end

group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end

Windows does not include zoneinfo files, so bundle the tzinfo-data gem

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

zhengpd 回复

!天哪您这么一说好像确实没有 edit gemfile……是我侠了。立刻改一下试试

加油 冲起来

需要 登录 后方可回复, 如果你还没有账号请 注册新账号