ruby-on-rails - Rails-在创建操作期间根据表单输入重定向到不同的路径
问题描述
我的控制器中有一个创建操作。
def create
@client=Client.find(params[:client_id])
@comment= @client.build_comment( comment_params )
if @comment.save
flash[:success]= "Thank you!"
redirect_to path_one
else
render action: :new
end
end
private
def comment_params
params.require(:comment).permit(:response, :experience)
end
现在在我的创建操作中,每当我的客户提交表单时,我希望能够根据“经验”的值重定向到不同的路径。
所以,如果体验是“积极的”,我希望他们去 path_one,如果体验是“消极的”,我希望他们去 path_two。
我试过这个:
def create
@client=Client.find(params[:client_id])
@comment= @client.build_comment( comment_params )
if @comment.save
if params[:experience]=="positive"
flash[:success]= "Thank you!"
redirect_to path_one
else
render action: :new
else
redirect_to path_two
end
end
end
private
def comment_params
params.require(:comment).permit(:response, :experience)
end
但这总是重定向到相同的路径。
解决方案
您的 if 语句的else
部分顺序错误。尝试这个:
if @comment.save
if params[:experience]=="positive"
flash[:success]= "Thank you!"
redirect_to path_one
else
redirect_to path_two
end
else
render action: :new
end
推荐阅读
- android - AlertDialog 中的 EditText 不会自动聚焦于 Android 9 (API 28)
- php - 我在 PHP 包含变量时遇到 include failed to open stream 错误
- c# - c# 广度优先搜索在迷宫中查找项目
- sidr - 用户单击链接时关闭 sidr 菜单
- c# - 当 Parallel.For 尚未完成时,如何处理 WM_PAINT?
- c# - 无法订阅节点 RosSharp C# Unity
- javascript - 为什么 setTimeout 返回数值
- r - 将新列添加到数据框中,它是 for 循环 R 中数据框的名称?
- java - Minecraft - 物品栏上方的信息栏完全人工制作
- formatting - 如何格式化数字向量和字符串向量?(Dyalog APL)