rails 3的新手
我想创建一个人们填写的联系表单,将其保存到数据库中,然后出现一个感谢页面。
我想在没有脚手架的情况下这样做,所以我可以更好地学习,并且我认为通过这样做可以更容易设置,这样人们就不能通过修改它来尝试查看其他人的条目网址。
理想情况下,它会将自己的状态保留在会话或Cookie中,以便在他们回来时最终会显示在感谢页面上。
一直尝试这样做大约3天并且阅读/谷歌搜索吨,但是在新路线之间redirect_to控制器东西在rails3中已经设法弄明白了。
的routes.rb
Contact::Application.routes.draw do
resources :contactees, :only => [:new, :create]
# to make sure crud doesn't have the routest I don't want
get 'contactees/submitted'
root :to => 'contactees#new'
contactees_controller.rb
ContacteesController < ApplicationControler
def share
end
def new
@contactee = Contactee.new
end
def create
@contactee = Contactee.new(params[:contactee])
if @contactee.save
redirect_to submitted_contactee
else
render action: "new"
end
end
end
视图
contactees
_form.html.erb
new.html.erb
submitted.html.erb
答案 0 :(得分:0)
摆脱提交的路线,你不需要它。也许是这样的?
def new
render :submitted if session[:contacted]
end
def create
@contactee = Contactee.new(params[:contactee])
if @contactee.save
session[:contacted] = true
render :submitted
else
render action: "new"
end
end