Ruby on rails 缺少参数或值为空:post
我是rails新手,我尝试为我的用户发布帖子,但编辑选项有一个错误。“创建”、“索引”、“显示”和“删除”功能正常,但“编辑”功能无效,出现以下错误: 参数缺失或值为空:post(在我的post_参数上) my_form.html.erb(用于创建和编辑):Ruby on rails 缺少参数或值为空:post,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我是rails新手,我尝试为我的用户发布帖子,但编辑选项有一个错误。“创建”、“索引”、“显示”和“删除”功能正常,但“编辑”功能无效,出现以下错误: 参数缺失或值为空:post(在我的post_参数上) my_form.html.erb(用于创建和编辑): @当前用户id%> 我的帖子: class PostsController < ApplicationController before_action :logged_in?, only: [:index, :show, :n
@当前用户id%>
我的帖子:
class PostsController < ApplicationController
before_action :logged_in?, only: [:index, :show, :new, :create, :edit, :update, :destroy]
def index
@posts = Post.paginate(page: params[:page])
end
def show
@post = find_post
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
flash[:success] = "Annonce créée avec succès"
redirect_to @post
else
render 'new'
end
end
def edit
@post = Post.update(post_params)
end
def update
@post = Post.update(post_params)
if @post.save
flash[:success] = "Annonce modifiée avec succès"
redirect_to @post
else
render 'edit'
end
end
def destroy
Post.find(params[:id]).destroy
flash[:success] = "Annonce supprimée avec succès"
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:content, :user_id)
end
def find_post
@post = Post.find(params[:id])
end
end
class PostsController
我尝试了很多东西,但都不起作用,错误在1和:之间发生变化:表单中的第一个参数不能包含nil或为空
提前感谢您的帮助请按照正确的方法操作
以正确的方式查看控制器
class PostsController < ApplicationController
before_action :logged_in?
before_action :find_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all.paginate(page: params[:page])
end
def show
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
flash[:success] = "Annonce créée avec succès"
redirect_to @post
else
render 'new'
end
end
def edit
end
def update
if @post.save
flash[:success] = "Annonce modifiée avec succès"
redirect_to @post
else
render 'edit'
end
end
def destroy
@post.destroy
flash[:success] = "Annonce supprimée avec succès"
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:content, :user_id)
end
def find_post
@post = Post.find(params[:id])
end
end
编辑操作不应用于更新,它应用于获取将被更新的对象,以便更改编辑操作
另外,在执行任何操作之前,请首先确保用户已登录。
在执行任何操作之前,显示在执行操作之前:登录。
应位于操作之前:查找\u post
before_action :logged_in?
before_action :find_post, only: [:show, :edit, :update, :destroy]
def edit
#@post = Post.update(post_params)
end
def update
@post = Post.update(post_params)
if @post.save
flash[:success] = "Annonce modifiée avec succès"
redirect_to @post
else
render 'edit'
end
end
还要确保user\u id:@current\u user.id
或current\u user.id%>
这取决于您以何种方式定义了
当前用户
如果身份验证id由设计
gem完成,那么它应该是当前用户
您可以显示stacktrace吗?@BenjaminRichard我希望,答案中给出的解释会提醒您以后不要犯此类错误。很高兴听到它:)
<%= f.hidden_field :user_id, :value => current_user.id %>
before_action :logged_in?
before_action :find_post, only: [:show, :edit, :update, :destroy]
def edit
#@post = Post.update(post_params)
end
def update
@post = Post.update(post_params)
if @post.save
flash[:success] = "Annonce modifiée avec succès"
redirect_to @post
else
render 'edit'
end
end