参数缺失或值为空:Postscontroller#new中的post

时间:2018-07-10 10:10:36

标签: ruby-on-rails

我正在尝试创建一个包含帖子和内容的网站。

目前,我有一个包含其组件的Post Model。

每当我尝试打开帖子/新消息时,就会出现此错误消息

ActionController::ParameterMissing in PostsController#new
param is missing or the value is empty: post

post_controller

def new
      @post = current_user.posts.build(post_params)
    if @post.save!
      redirect_to @post
    else

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

我以前能够在localhost:3000/post/new上创建帖子。我只是想再试一次,意识到我一定搞砸了。 这是我的帖子表格。

<div class="form-control">
  <div class="card-my-4">
    <%= form_for Post.new do |f| %>
      <div class="card-body">
        <strong>post title: </strong>
        <%= f.text_area :title, :maxlength => 40, class: 'form-control', rows: 1, id: 'textareaedit' %>
      </div>

      <div class="card-body">
        <strong>Compose your Quote content: </strong>
        <div class="form-group">
          <%= f.text_area :body, :maxlength => 240, class: 'form-control', rows: 3 %>
        </div>
        <hr>
        <%= submit_tag "Submit", class: "btn btn-outline-success" %>
    <% end %>
    </div>
  </div>
</div>

post.rb

class Post < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy

  validates :title, presence: true, length: { minimum: 5 }
  validates :body, presence: true, length: { minimum: 240 }
end

1 个答案:

答案 0 :(得分:0)

尝试更改您的代码:

post_controller:

def create
  @post = current_user.posts.build(post_params)
  if @post.save!
    redirect_to @post
  else
  ....
end
def new
  @post = Post.new
end

观看次数:

<div class="form-control">
  <div class="card-my-4">
    <%= form_for @post do |f| %>
    ............