我正在使用Rails創建基本產品登錄頁面,用戶可以在其中輸入他們的電子郵件地址,以便在產品啓動時收到通知。 (是的,有服務/寶石等可以爲我做這件事,但我是編程新手,並且想自己構建它來學習rails。)如何爲表單確認頁面定義自定義URL?
成功提交表單後,我想重定向到我感謝用戶對產品的興趣(並鼓勵他們完成一項簡短的調查)的定製「感謝」頁面。
目前,成功提交顯示在「/ invites /:id /」例如「邀請/ 3」,我不想要,因爲它暴露了已提交的邀請數。我想將所有成功的提交重定向到「/謝謝」頁面。
我試圖研究「導軌自定義URL」,但一直未能找到任何有效的工具。我能找到的最接近的是Stackoverflow post on how to redirect with custom routes,但並未完全理解推薦的解決方案。我也嘗試閱讀Rails Guide on Routes,但對此我很陌生,沒有看到任何我瞭解允許創建自定義URL的內容。
我已經把我想顯示成功的形式提交的「意見/邀請/ show.html.haml」
我的routes文件
resources :invites
root :to => 'invites#new'
我想我的謝意消息插入routes.rb:
post "/:thanks" => "invites#show", :as => :thanks
但我不知道這是否會工作,或者我會告訴控制器奧勒重定向到:感謝
我控制器(基本上香草軌,這裏僅包括相關的動作):
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這似乎。任何意見如何實現這一點,將不勝感激。
這工作!謝謝! – andersr 2013-04-10 20:29:54