Ruby on rails rails参数缺失或值为空:post
我已经在这个问题上停留了一段时间,并且已经寻找了一个又一个解决方案,到目前为止,我遇到的那些都不起作用 我为我的应用程序的一个新对象制作了一个新的rails脚手架,我已经设置了表,并且我可以在运行服务器时查看Post对象 当我在服务器运行时查看Post对象时出现问题,它的内容不显示,并且我无法更新引发此错误的对象:Ruby on rails rails参数缺失或值为空:post,ruby-on-rails,Ruby On Rails,我已经在这个问题上停留了一段时间,并且已经寻找了一个又一个解决方案,到目前为止,我遇到的那些都不起作用 我为我的应用程序的一个新对象制作了一个新的rails脚手架,我已经设置了表,并且我可以在运行服务器时查看Post对象 当我在服务器运行时查看Post对象时出现问题,它的内容不显示,并且我无法更新引发此错误的对象: param is missing or the value is empty: post 数据库中的Posts表如下所示: 如您所见,字段content包含我想在查看上述帖子时显
param is missing or the value is empty: post
数据库中的Posts表
如下所示:
如您所见,字段content
包含我想在查看上述帖子时显示在页面上的数据
而是出现了这样的情况:
如您所见,它不会显示content
字段的内容
这是我的posts\u controller.rb
class PostsController < ApplicationController
# you may want to udpate these posts? typo's outdated info?
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: @post }
else
format.html { render action: 'new' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:content)
end
end
我认为这就是定义:post
的原因,因为在操作:set\u post之前,只有:[显示,:编辑,:更新,:销毁]
def set_post
@post = Post.find(params[:id])
end
这是rails中错误页面上显示的内容:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"tHFQAWv08aUoi7ndZsVC1aCNJkYP6hE8w+89NTTpBjc=",
"commit"=>"Update Post",
"id"=>"2"}
我真的很困惑为什么会发生这种情况,我是rails的新手,所以毫无疑问我遗漏了一些基本的东西
谢谢,
Chris.您的请求缺少
post
参数:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"tHFQAWv08aUoi7ndZsVC1aCNJkYP6hE8w+89NTTpBjc=",
"commit"=>"Update Post",
"id"=>"2"}
这很可能是由于app/views/posts/edit.html.erb
视图中的表单中存在错误。表单应如下所示:
<%= form_for @post, url: {action: "edit"} do |f| %>
<%= f.text_area :content %>
<%= end %>
您可以在中阅读更多关于表单的信息。您能把html代码放在这里吗?因为我认为这就是问题所在is@OmarMowafi现在不用了!你提到这一点让我想起我需要更新html所以谢谢!不客气。我认为你应该结束这个问题,或者用你为其他人遗漏的东西来更新它。
<%= form_for @post, url: {action: "edit"} do |f| %>
<%= f.text_area :content %>
<%= end %>