gpt4 book ai didi

ruby-on-rails - rails 错误 'First argument in form cannot contain nil or be empty'

转载 作者:太空宇宙 更新时间:2023-11-03 16:21:50 25 4
gpt4 key购买 nike

我已经尝试了所有关于如何消除错误的 Stack 帖子 '形式中的第一个参数不能包含 nil 或为空',他们的解决方案都不适合我。

我的应用涉及发帖,用户可以在其中上传图片。我希望他们能够直接从他们的文件系统上传,而不是获取图片 url。

这是我的 Controller :

  class PostsController < ApplicationController
before_action :all_posts, only: [:feed, :create]
respond_to :html, :js

def show
@post = Post.find_by_id(params['id'])
end


def create
@post = Post.create(post_params)
end

def new
@post = Post.new
end

private

def all_posts
@posts = Post.all
end

def post_params
params.require(:post).permit(:caption, :content, :image)
end

end

和 new.html.erb:

<footer>
<%= form_for @post, :html => {:multipart => true} do |f| %>
<%= f.text_field :caption %>
<%= f.text_field :content %>
<%= f.file_field :image %>
<%= f.submit %>
<% end %>
</footer>

人们一直建议将@post = Post.new 添加到表单和操作中,但没有一个对我有用。

此外,我已经检查了我的数据库,目前我有 7 个帖子。

非常感谢您的帮助。

最佳答案

你得到这个错误是因为当你调用 form_for(@post,.... 时你的 @postnil 中p>

您必须在 Controller 的相应操作中实例化 @post 实例变量。由于您现在没有它,因此您会收到此错误。

通过查看您的其余代码,我认为您希望将帖子上传到您的 new.html.erb 文件中,而不是 feed.html.erb问题中提到。

关于ruby-on-rails - rails 错误 'First argument in form cannot contain nil or be empty',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32171728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com