- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在为 Rails 应用构建联系表单,但出现以下错误:
'The action 'index' could not be found for ContactsController'
我很困惑,因为我以前建立过联系表格,方式相同,没有问题。这似乎很奇怪,因为我根本没有在我的 ContactsController 中使用或引用 index.html.erb 或索引方法?请找到以下文件:
contacts_controller.rb
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
if @contact.save
flash[:success] = "Message Sent!"
redirect_to new_contact_path
else
flash[:danger] = "Error occurred"
redirect_to new_contact_path
end
end
private
def contact_params
params.require(:contact).permit(:name, :phone, :email, :comments)
end
end
模型/contact.rb
class Contact < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true
after_create :send_email
private
def send_email
ContactMailer.contact_email(self).deliver
end
end
邮寄者/contact_mailer.rb
class ContactMailer < ActionMailer::Base
default to: 'example@example.com.au'
def contact_email(contact)
@contact = contact
mail(from: @contact.email, subject: 'Contact Form Message').deliver
end
end
views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>RunpixelrunWebsite</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %> alert-dismissable">
<button type="button" class ="close" data-dismiss="alert" aria-label="Close"><span aria-hidden = "true">×</span></button>
<%= value %>
</div>
<% end %>
<%= yield %>
</div>
</body>
</html>
views/contacts/new.html.erb
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well">
<%= form_for @contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :phone %>
<%= f.text_field :phone, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit "Submit", class: "btn btn-default" %>
<% end %>
</div>
</div>
</div>
路线.rb
Rails.application.routes.draw do
resources :contacts
root 'contacts#new'
日志:
Started POST "/contacts" for 10.240.0.44 at 2015-11-19 05:25:05 +0000
Cannot render console from 10.240.0.44! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ContactsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ruEgewytZuJBEBh2bHoJJWj9uY4dp69JQOVFyl/V+v9l+dBTi3ZQn1Iq8Mb4D6bUSLDAvG9dNMSCKLVrfsDAKw==", "contact"=>{"name"=>"asdf", "phone"=>"asdf", "email"=>"asdfadsf@sfdgasd", "comments"=>"asdfasdf"}, "commit"=>"Submit"}
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "contacts" ("name", "phone", "email", "comments", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["name", "asdf"], ["phone", "asdf"], ["email", "asdfadsf@sfdgasd"], ["comments", "asdfasdf"], ["created_at", "2015-11-19 05:25:05.377425"], ["updated_at", "2015-11-19 05:25:05.377425"]]
DEPRECATION WARNING: `#deliver` is deprecated and will be removed in Rails 5. Use `#deliver_now` to deliver immediately or `#deliver_later` to deliver through Active Job. (called from send_email at /home/ubuntu/workspace/runpixelrun_website/app/models/contact.rb:9)
Rendered contact_mailer/contact_email.html.erb (1.6ms)
Sent mail to example@example.com.au (30014.0ms)
Date: Thu, 19 Nov 2015 05:25:05 +0000
From: asdfadsf@sfdgasd
To: example@example.com.au
Message-ID: <564d5d319e2ca_56b22a3201c112db@runpixelrun-runpixelrun_projects-2170203.mail>
Subject: Contact Form Message
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE Html>
<html>
<head>
<title></title>
</head>
<body>
<p>New message from RuNpiXelruN's Contact Form!, from asdf, asdfadsf@sfdgasd</p>
</br>
<p>asdf</p>
<p>asdf</p>
<p>asdfasdf</p>
</body>
</html>
ContactMailer#contact_email: processed outbound mail in 30263.5ms
Sent mail to example@example.com.au (30006.4ms)
Date: Thu, 19 Nov 2015 05:25:05 +0000
From: asdfadsf@sfdgasd
To: example@example.com.au
Message-ID: <564d5d319e2ca_56b22a3201c112db@runpixelrun-runpixelrun_projects-2170203.mail>
Subject: Contact Form Message
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE Html>
<html>
<head>
<title></title>
</head>
<body>
<p>New message from RuNpiXelruN's Contact Form!, from asdf, asdfadsf@sfdgasd</p>
</br>
<p>asdf</p>
<p>asdf</p>
<p>asdfasdf</p>
</body>
</html>
(18.5ms) commit transaction
Redirected to http://runpixelrun-projects-runpixelrun.c9users.io:8080/contacts/new
Completed 302 Found in 60313ms (ActiveRecord: 19.0ms)
Started GET "/contacts" for 10.240.0.45 at 2015-11-19 05:28:48 +0000
Cannot render console from 10.240.0.45! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
AbstractController::ActionNotFound (The action 'index' could not be found for ContactsController):
actionpack (4.2.4) lib/abstract_controller/base.rb:132:in `process'
actionview (4.2.4) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.4) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.4) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `call'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.4) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.4) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.4) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:31:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.4) lib/rails/engine.rb:518:in `call'
railties (4.2.4) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb within rescues/layout (0.6ms)
这一切看起来都在发送和被调用,好吗?
谢谢
贾斯汀
最佳答案
如果您正在使用资源。您应该在 Controller 中执行所有这些操作。
GET /photos photos#index display a list of all photos
GET /photos/new photos#new return an HTML form for creating a new photo
POST /photos photos#create create a new photo
GET /photos/:id photos#show display a specific photo
GET /photos/:id/edit photos#edit return an HTML form for editing a photo
PATCH/PUT /photos/:id photos#update update a specific photo
DELETE /photos/:id photos#destroy delete a specific photo
在你的 route 代替 resources :contacts
。使用资源:联系人,仅:[:创建,:新]
或
资源:联系人,除了:[:index]
在上面的示例路由中指定您需要的方法。在 resources :contacts, only: [:create,:new]
中的第一个中,您的 Controller 中应该只有两个操作。
GET /photos/new photos#new return an HTML form for creating a new photo
POST /photos photos#create create a new photo
如果你不想限制你的路线。在 Controller 中指定 Action :对于 Controller 中的 index
操作就像这样。这也很有效。
def index
end
关于ruby-on-rails - rails : The action 'index' could not be found for ContactsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33796078/
以下是一个非常简单的ruby服务器。 require 'socket' local_socket = Socket.new(:INET, :STREAM) local_addr = Socket.
我正在使用 OS X(使用 bash),并且是 unix 的新手。我想知道是否可以修改一些文件以便运行 ruby 程序,我不需要“ruby file.rb”,而是可以运行“ruby.rb”。 有理
我在用 Ruby 替换字符串时遇到一些问题。 我的原文:人之所为不如兽之所为。 我想替换为:==What== human does is not like ==what== animal does.
我想在一个循环中从 Ruby 脚本做这样的事情: 写一个文件a.rb(每次迭代都会改变) 执行系统(ruby 'a.rb') a.rb 将带有结果的字符串写入文件“results” a.rb 完成并且
我的问题是尝试创建一个本地服务器,以便我可以理解由我的新团队开发的应用程序。我的问题是我使用的是 Ruby 2.3.3,而 Gemfile 需要 2.3.1。我无法编辑 Gemfile,因为我被告知很
我有一个使用 GLI 框架用 Ruby 编写的命令行实用程序。我想在我的主目录中配置我的命令行实用程序,使用 Ruby 本身作为 DSL 来处理它(类似于 Gemfile 或 Rakefile)。 我
我的 Rails 应用 Controller 中有这段代码: def delete object = model.datamapper_class.first(:sourced_id =>
我正在寻找的解析器应该: 对 Ruby 解析友好, 规则设计优雅, 产生用户友好的解析错误, 用户文档的数量应该比计算器示例多, UPD:允许在编写语法时省略可选的空格。 快速解析不是一个重要的特性。
我刚开始使用 Ruby,听说有一种“Ruby 方式”编码。除了 Ruby on Rails 之外,还有哪些项目适合学习并被认可且设计良好? 最佳答案 Prawn被明确地创建为不仅是一个该死的好 PDF
我知道之前有人问过类似的问题,但是我该如何构建一个无需在前面输入“ruby”就可以在终端中运行的 Ruby 文件呢? 这里的最终目标是创建一个命令行工具包类型的东西。现在,为了执行我希望用户能够执行的
例如哈希a是{:name=>'mike',:age=>27,:gender=>'male'}哈希 b 是 {:name=>'mike'} 我想知道是否有更好的方法来判断 b 哈希是否在 a 哈希内,而
我是一名决定学习 Ruby 和 Ruby on Rails 的 ASP.NET MVC 开发人员。我已经有所了解并在 RoR 上创建了一个网站。在 ASP.NET MVC 上开发,我一直使用三层架构:
最近我看到 Gary Bernhardt 展示了他用来在 vim 中执行 Ruby 代码的 vim 快捷方式。捷径是 :map ,t :w\|:!ruby %. 似乎这个方法总是执行系统 Rub
在为 this question about Blue Ruby 选择的答案中,查克说: All of the current Ruby implementations are compiled to
我有一个 Ruby 数组 > list = Request.find_all_by_artist("Metallica").map(&:song) => ["Nothing else Matters"
我在四舍五入时遇到问题。我有一个 float ,我想将其四舍五入到小数点后的百分之一。但是,我只能使用 .round ,它基本上将它变成一个 int,意思是 2.34.round # => 2. 有没
我使用 ruby on rails 编写了一个小型 Web 应用程序,它的主要目的是上传、存储和显示来自 xml(文件最多几 MB)文件的结果。运行大约 2 个月后,我注意到 mongrel 进程
我们如何用 Ruby 转换像这样的字符串: 𝑙𝑎𝑡𝑜𝑟𝑟𝑒 收件人: Latorre 最佳答案 s = "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" => "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" s.u
通过 ruby monk 时,他们偶尔会从左侧字段中抛出一段语法不熟悉的代码: def compute(xyz) return nil unless xyz xyz.map {|a,
不确定我做错了什么,但我似乎弄错了。 问题是,给你一串空格分隔的数字,你必须返回最大和最小的数字。 注意:所有数字都是有效的 Int32,不需要验证它们。输入字符串中始终至少有一个数字。输出字符串必须
我是一名优秀的程序员,十分优秀!