Ruby on rails Rails-在保留表单数据的同时,如何在注释错误后重定向回post?
我有一个基本的帖子/评论设置。在这里,我称它们为pin和reply,但分别采用相同的格式 我的回复(评论)嵌套在PIN(帖子)下。当我提交一个带有错误的回复时,我会重定向回pin,但表单上输入的任何数据都会丢失。我想保留这个 回复\u controller.rbRuby on rails Rails-在保留表单数据的同时,如何在注释错误后重定向回post?,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我有一个基本的帖子/评论设置。在这里,我称它们为pin和reply,但分别采用相同的格式 我的回复(评论)嵌套在PIN(帖子)下。当我提交一个带有错误的回复时,我会重定向回pin,但表单上输入的任何数据都会丢失。我想保留这个 回复\u controller.rb def create @pin = Pin.find(params[:pin_id]) @reply = @pin.replies.build(params[:reply].permit(:reply)) @
def create
@pin = Pin.find(params[:pin_id])
@reply = @pin.replies.build(params[:reply].permit(:reply))
@reply.user_id = current_user.id
@reply.save
if @reply.save
flash[:success] = "Reply successfully created"
redirect_to pin_path(@pin)
else
if @reply.errors.any?
flash[:warning] = "#{@reply.errors.count} Problems found: #{@reply.errors.full_messages.to_sentence}"
end
redirect_to pin_path(@pin)
end
end
因为它执行重定向,我认为这就是擦除表单的过程。我如何处理错误,使其呈现我刚刚来自的pin(post)视图,而不是重定向
非常感谢!
Michael。不要重定向,而是:
渲染“pins/new”
使用渲染
而不是重定向
def create
@pin = Pin.find(params[:pin_id])
@reply = @pin.replies.build(params[:reply].permit(:reply))
@reply.user_id = current_user.id
@reply.save
if @reply.save
flash[:success] = "Reply successfully created"
redirect_to pin_path(@pin)
else
if @reply.errors.any?
flash[:warning] = "#{@reply.errors.count} Problems found: #{@reply.errors.full_messages.to_sentence}"
end
render :template => "pins/show"
end
end
这不管用。这不是我想展示的新的回应行动。这个答复是用别针写的。它需要呈现我刚才试图回复的pin。呈现show模板会给我以下信息:缺少模板/显示{:locale=>[:en],:formats=>[:html],:handlers=>[:erb,:builder,:raw,:ruby,:jbuilder,:coffee]}。它是在寻找答案吗?因为我需要渲染刚才看到的pin的显示。例如,我可能一直在/pins/5上回复,需要返回那里!这样做会导致错误的操作。我试图呈现我刚才回复的pin的视图。例如,我在/pins/5上回复,我需要回到那里,显示回复错误。