"form_with cannot find the Post model"

"form_with cannot find the Post model"

我正在尝试在我的“新”页面上创建表单。我正在使用 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 

您的代码片段中存在很多空白问题,但我认为这可能是您复制代码的副作用,而不是任何故意的原因。

您看到的错误是因为 form_with 方法不知道 post 是什么。在您的控制器中,您在 new 方法中设置了 @post,这意味着 new.html.erb 将能够看到实例变量 @post,但它不知道是什么post(没有 @)是。

因此,如果表单在 new.html.erb 中,您需要在此处使用 @post 代替 post

也就是说,常见的 Rails 行为是在其自己的部分 _form.html.erb 中包含表单。这里可能就是这种情况(您没有指定视图的文件名)。在这种情况下,您可以告诉部分使用局部变量而不是主模板文件使用的实例变量

为此,您需要在对表单的 render 调用中将实例变量映射到本地变量,例如:

# in new.html.erb
<%= render 'form', post: @post` %>

这是说'take the object that I can see as @post, and make it available as the post local variable within the form template'。

替换

<% = form_with(model: post, location: true) do | form | %>
.....
<% end %>

<% = form_with(model: post, local: true) do | form | %>
....
<% end %>

new.html.erb应该包含

<%= render 'form', post: @post` %>

并使用 posts_controller.rb

更新您的控制器
class PostsController < ApplicationController
before_action :find_post, only: [:show]

 def index
    @posts = Post.all
  end

  def new
    @post = Post.new
  end

  def show
   
  end

  def create
    @post = Post.new(post_params)
    @post.save
    redirect_to @post
   end
 
private
 def find_post
   @post = Post.find(params[:id])
 end

  def post_params
    params.require(:post).permit(:title, :author, :body)
  end
end

Routes 应该是

get 'posts', to: 'posts#index'
get 'posts/new', to: 'posts#new'
get 'posts/:id', to: 'posts#show'
post 'posts', to: 'posts#create'

或简称为

resources :posts, only: [:index, :new, :show, :create]