- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我是 Rails 的新手,我遇到了这个错误:
undefined method `posts_path' for #<#<Class:0x007fe3547d97d8>:0x007fe3546d58f0>
我已经在下面发布了我的文件,请记住我是 Rails 的新手,所以非常感谢简单的解释!
路线.rb:
Rails.application.routes.draw do
get '/post' => 'post#index'
get '/post/new' => 'post#new'
post 'post' => 'post#create'
end
post_controller.rb:
class PostController < ApplicationController
def index
@post = Post.all
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to '/post'
else
render 'new'
end
end
private
def post_params
params.require(:post).permit(:content).permit(:title)
end
end
新的.html.erb:
<%= form_for(@post) do |f| %>
<div class="field">
<%= f.label :post %><br>
<%= f.text_area :title %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit "Create" %>
</div>
<% end %>
最佳答案
我猜 form_for(@post)
期望有一个名为 posts_path
的方法,但一个不存在,因为它没有在你的路由中定义文件。尝试替换:
Rails.application.routes.draw do
get '/post' => 'post#index'
get '/post/new' => 'post#new'
post 'post' => 'post#create'
end
与
Rails.application.routes.draw do
resources :posts, only: [:new, :create, :index]
end
编辑:更多信息:
在 http://guides.rubyonrails.org/form_helpers.html 阅读有关表单助手的完整页面,特别是阅读“2.2 将表单绑定(bind)到对象”部分以及以下内容:
When dealing with RESTful resources, calls to form_for can get significantly easier if you rely on record identification. In short, you can just pass the model instance and have Rails figure out model name and the rest:
## Creating a new article
# long-style:
form_for(@article, url: articles_path)
# same thing, short-style (record identification gets used):
form_for(@article)
## Editing an existing article
# long-style:
form_for(@article, url: article_path(@article), html: {method: "patch"})
# short-style:
form_for(@article)Notice how the short-style form_for invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking record.new_record?. It also selects the correct path to submit to and the name based on the class of the object.
因此,无论是否有意,当您说 form_for(@post)
时,您是在要求 Rails 根据您的 的名称猜测您的表单应该提交到的路径>@post
变量。您定义的路线与 rails 预期的路线不匹配。
有关 Rails 中路由的更多信息,请阅读 http://guides.rubyonrails.org/routing.html 上的整个页面,并特别注意“2 资源路由:Rails 默认值”部分。您的 form_for(@post)
将假设您正在使用“资源路由”,这是我切换到的。
至于为什么你得到一个新的错误?在您的应用程序中的其他地方,您希望使用以前的自定义路由,而现在您使用的是 Rails“资源路由”,因此您的路径名称会有所不同。 No route matches [GET] "/post/new"因为现在路由匹配 No route matches [GET] "/posts/new"(注意复数 posts)。
关于ruby-on-rails - #<#<Class :0x007fe3547d97d8>:0x007fe3546d58f0> 的未定义方法 `posts_path',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39905556/
我目前正在使用 Cypress 测试几个微服务。为了测试 REST api,我需要一个 token 来发出请求。该 token 只能通过本地 FE 获得 - 下面给出的图像 我必须访问此 FE 才能仅
我用一些 FE 插件开发了 TYPO3 (6.2) 扩展。 我需要更改有关插件的信息,该信息显示在页面 View 的后端。 现在只显示插件的标题和名称... 我已经使用 flexforms 来配置插件
我刚得到一份 FE 开发人员的工作邀请,但我以前从未听说过这个术语。需要强大的 HTML、CSS、Javascript、jQuery、XSL 技能。 我认为自己精通所有这些领域,但 XSL 除外,我以
我用 C 编程,在那里我使用库 libpq-dev/libpq-fe 。通过这个库,我连接到本地 postgresql 数据库。 我写了一些测试代码,它选择/插入一些东西。但我一直有一个问题:我只能选
我这辈子都弄不明白。 当我 pip install django-tenant-schemas 时,它会尝试安装需要 Python header 和 gcc 的依赖项 psycopg2。我已经安装了所
我正在使用 C 中的 libpq-fe 库来连接到 postgres 数据库。 我正在使用返回时间戳的 libpqfe 从我的代码中调用存储过程。 我想将这个时间戳存储到一个 char 数组中。但要这
我面临以下问题:我正在尝试将 FIWARE-Perseo 作为 docker 实例部署到我的 Centos 7 服务器。尽管 perseocore 实例运行没有问题,但它与 perseo 前端的情况不
我创建了一个 2010 Access 数据库供我的团队使用。现在数据库位于服务器上的共享文件夹中,每个人都使用相同的数据库文件。我最近在数据库自动备份和创建额外文件(Database.mdb、Data
我在很多地方都看到过这个错误,但是没有人回答需要做什么来修复它或者为什么会发生。我希望我能有更好的运气。 ConsentInformation consentInformation = Consent
场景: 我正在尝试提出一种在 JS 中实现版本控制的方法,它满足两个前提: 缓存方面 很少(或理想情况下没有)调用后端 于是我想出了这个方案: 每次有新版本可用时,我都需要 FE 重新加载内容,因此实
在我的模型中的 TYPO3 6.2 中,我有一个名为 documents 的文件的公共(public)字段。 ,它是 ObjectStorage的 \TYPO3\CMS\Extbase\Domain\
我试图编译pgmodeler 0.7.2(https://github.com/pgmodeler/pgmodeler/releases)的源,但是我有问题: C:\Users\Krzysztof\D
我正在尝试在 postgres 上运行一个简单的查询,但无法获取 bigint 列的值。我尝试的所有操作都返回 0(零)或错误的值... 当我使用strtoll时,返回0(零) int64_t ret
我想在 windows 7 和 visual studio 2010 中编译一个 cpp 源文件 Microsoft (R) C/C++ 优化编译器版本 16.00.40219.01 for x64
我创建了一个系统,可以从车辆读取 CANBUS 数据并将其无线传输到“基站”。到目前为止,我的代码的工作原理是它通过数据线将东西发送到 xbee,然后 xbee 将它发送到接收模块,除了它发送的唯一东
如果我们想要进行社交认证(例如Facebook或Google),那么哪一方最好集成它。后端侧还是前端侧?对于后端,我们有NodeJS,其中我们可以使用PassportJS来完成它,对于前端,我们有An
我有两个域指向同一个网站,域 A 和域 B。我想要: 可通过 HTTP 访问域 A(开箱即用) 域 B 将所有请求重定向到 HTTPS。基本上如果你输入 http://domainb.com/some
因此,我有一个带有 SQL BE 的 Access 2007 FE。我正在使用绑定(bind)表单来添加新记录或更新现有记录。 我的问题是:与本地 Access 表不同,使用 SQL 表会导致自动编号
在搜索了很多答案并尝试了一些不同的方法都没有成功之后,我真的是出于绝望而发布了这个。 我有一个 Access 数据库,我最近将其中的表迁移到 SQL 2005,Access 继续作为提供表单、报告和查
我通常不在论坛上发帖,因为我总能在不打扰任何人的情况下找到一些东西。无论如何,这次我没有运气。 我为我的网站编写了一个 DB-Communications-System,您可以在其中通过 Java 应
我是一名优秀的程序员,十分优秀!