【发布时间】:2021-08-05 11:48:39
【问题描述】:
我正在尝试在我的“新”页面上创建表单。我正在使用 form_with。我已经检查了我的路由、控制器和视图,但我还没有发现问题。
返回的错误是:
NameError in Pages # new
undefined local variable or method `post 'for # <# <Class: 0x00007f5640e1f440>: 0x00007f5640e1c060>
Did you mean? @post
Extracted source (around line # 5):
<% = form_with (model: post, location: true) do | form | %>
<div class = "form-group">
<% = form.label: title%>
以下是我使用 form_with 的表单:
<h1> CREATE NEW ARTICLE </h1>
<% = form_with (model: post, location: true) do | form | %>
<div class = "form-group">
<% = form.label: title%>
<% = form.text_field: title, class: 'form-control'%>
</div>
<div class = "form-group">
<% = form.label: author%>
<% = form.text_field: author, class: 'form-control'%>
</div>
<div class = "form-group">
<% = form.label: body%>
<% = form.text_area: body, class: 'form-control', rows: 10%>
</div>
<% = form.submit class: 'btn btn-primary', data: {disable_with: 'Creating ..'}%>
<% end%>
这是我的控制器:
class PagesController <ApplicationController
def articles
@posts = Post.all
end
def new
@post = Post.new
end
def show
@post = Post.find (params [: id])
end
def create
@post = @ Post.new (post_params)
@ post.save
redirect_to article_path (@post)
end
private
def post_params
params.require (: post) .permit (: title,: author,: body)
end
end
现在这是我的路线:
Rails.application.routes.draw do
root 'pages # index'
get 'articles', to: 'pages # articles'
get 'articles / new', to: 'pages # new'
get 'articles /: id', to: 'pages # show', as: 'article'
post 'articles', to: 'pages # create'
end
【问题讨论】:
-
你真的有post模型吗?
-
是的。包括我可以毫无问题地在控制台上创建帖子
标签: ruby-on-rails methods model routes controller