我一直试图解决这个问题一段时间,但还没到任何地方。如果有人能让我知道如何做到这一点,或者我有什么方法可以使用render
方法来代替(目前保留错误但重定向到错误的路径,如下所述... )
我有一个表单的自定义路由,当有验证错误时我试图重定向:
get "clubs/sign_up/:plan_id", to: "clubs#new", as: :new_membership
下面是我目前在我的控制器中的内容,以及关于我尝试过的其他步骤的一些评论
clubs_controller.rb
def create
@membership = Membership.new(membership_params)
if @membership.save
redirect_to root_path
else
flash[:error] = "Please check form errors:"
redirect_to new_membership_path(session[:membership_plan_id]) #errors lost
# render action: 'new', plan_id: 'silver' # <<<Preserves errors but breaks path, renders: localhost:3000/clubs instead of .../clubs/sign_up/:plan_id
# session[:membership_errors] = @membership.errors #<<< Doesn't wotk either, getting a cookie overflow error when trying to pass errors to #new
return
end
end
def new
session[:membership_plan_id] = params[:plan_id]
@membership = Membership.new
end
答案 0 :(得分:1)
假设plan_I&#39; d是您模型的一部分..
将渲染线更改为:
render :new
将其余部分更改为:
def new
session[:membership_plan_id] = params[:plan_id]
@membership = Membership.new plan_id: params[:plan_id]
end
def create
@membership = Membership.new(membership_params)
if @membership.save
redirect_to root_path
else
flash[:error] = @membership.errors.full_messages.to_sentence
render :new
end
end
在表单中为plan_id添加隐藏字段。渲染错误的原因是它没有param可用,尝试将它添加到渲染操作不起作用因此你的问题
答案 1 :(得分:0)
您需要渲染而不是重定向。在这里使用(..snip..)
else
flash[:error] = "Problem with form"
new
end
end
def new
session[:membership_plan_id] = params[:plan_id]
@membership ||= Membership.new
render 'new'
end
运算符来帮助..它只是红宝石,所以像
dt1 <- fread("V1 V2 V3
x b;c;d 1
y d;ef 2
z d;ef 3")