如何为表单确认页面定义自定义URL? [英] How do I define a custom URL for a form confirmation page?
问题描述
我正在用Rails创建一个基本的产品登录页面,用户可以在其中输入其电子邮件地址,以在产品启动时得到通知。 (是的,有一些服务/宝石等可以为我做这件事,但是我是编程新手,想自己构建它以学习Rails。)
I am creating a basic product landing page with Rails in which users can enter their email address to be notified when the product launches. (Yes, there are services/gems etc that could do this for me, but I am new to programming and want to build it myself to learn rails.)
成功提交表单后,我想重定向到一个自定义的 /谢谢页面,在该页面中,我感谢用户对产品的关注(并鼓励他们完成简短的调查。)
On successful submit of the form, I would like to redirect to a custom '/thanks' page in which I thank users for their interest in the product (and also encourage them to complete a short survey.)
当前,成功提交的信息显示在 / invites /:id /,例如 invites / 3,我不希望看到它,因为它公开了已提交的邀请数量。我想将所有成功的提交重定向到 /谢谢页面。
Currently, successful submits are displayed at "/invites/:id/" eg "invites/3" which I do not want since it exposes the number of invites that have been submitted. I would like to instead redirect all successful submits to a "/thanks" page.
我试图研究 rails custom URLs,但是却找不到任何东西那个有效。我能找到的最接近的是该 Stackoverflow帖子,如何使用自定义路由重定向,但并未完全了解建议的解决方案。我也尝试阅读路线指南,但是对此并不陌生,什么也没看到我理解允许创建自定义URL。
I have attempted to research "rails custom URLs" but have not been able to find anything that works. The closest I was able to find was this Stackoverflow post on how to redirect with custom routes but did not fully understand the solution being recommended. I have also tried reading the Rails Guide on Routes but am new to this and did not see anything that I understood to allow for creating a custom URL.
我已将我要表示感谢信息的消息显示在 views / invites / show.html提交的成功表单中。 .haml
I have placed my thanks message which I would like displayed on successful form submit in "views/invites/show.html.haml"
我的路线文件
resources :invites
root :to => 'invites#new'
我尝试在route.rb中插入:
I tried inserting in routes.rb:
post "/:thanks" => "invites#show", :as => :thanks
但是我不知道这是否可行或如何告诉控制器重定向到:谢谢
But I don't know if this would work or how I would tell the controller to redirect to :thanks
我的控制器(基本上是香草导轨,此处仅包括相关操作):
My controller (basically vanilla rails, only relevant actions included here):
def show
@invite = Invite.find(params[:id])
show_path = "/thanks"
respond_to do |format|
format.html # show.html.erb
format.json { render json: @invite }
end
end
# GET /invites/new
# GET /invites/new.json
def new
@invite = Invite.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @invite }
end
end
# POST /invites
# POST /invites.json
def create
@invite = Invite.new(params[:invite])
respond_to do |format|
if @invite.save
format.html { redirect_to @invite }
#format.js { render :action => 'create_success' }
format.json { render json: @invite, status: :created, location: @invite }
else
format.html { render action: "new" }
#format.js { render :action => 'create_fail' }
format.json { render json: @invite.errors, status: :unprocessable_entity }
end
end
end
似乎创建用于显示确认的标准URL相对简单。
It would seem as if creating a standard URL for displaying a confirmation would be relatively straightforward. Any advice on how to achieve this would be appreciated.
推荐答案
我想您想在执行create操作后重定向。
I guess you want to redirect after your create action, which is executed when the form is submitted.
只需按以下方式添加redirect_to:
Just add redirect_to in the following way:
def create
@invite = Invite.new(params[:invite])
if @invite.save
...
redirect_to '/thanks'
else
...
redirect_to new_invite_path # if you want to return to the form submission page on error
end
end
为简洁起见,我省略了一些代码。
I omitted some of the code for brevity.
在您的路线中添加:
get '/thanks', to: "invites#thanks"
向您的邀请控制器添加感谢动作:
Add the thanks action to your invites controller:
def thanks
# something here if needed
end
并创建thank.html应用/视图/邀请中的.erb页面。
And create a thanks.html.erb page in app/views/invites.
这篇关于如何为表单确认页面定义自定义URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!