我正在尝试创建一个感谢页面,我的路线工作正常,因为我在 url 上对其进行了测试并且工作正常,但是当我尝试在创建操作中重定向时,我得到:
Routing Error
No route matches {:action=>"thank_you", :locale=>:en, :controller=>"appointments"}
控制器
def create
@appointment = Appointment.new(params[:appointment])
if @appointment.save
#send email
AppointmentMailer.appointment_confirmation(@appointment).deliver
AppointmentMailer.new_appointment(@appointment).deliver
redirect_to :action => "thank_you"
else
render :action => 'new', :alert => @appointment.errors.full_messages.split(', ')
end
end
def thank_you
@appointment = Appointment.find(params[:id])
end
路线
resources :appointments, :except => :new do
member do
get :thank_you
end
end
您需要将其添加为 RESTful 操作(或假设默认匹配路由)。
简而言之:
resources :appointments do
member do
get 'thank_you'
end
end
或:
resources :appointments do
get 'thank_you', :on => :member
end
要获取新页面,您必须做的不仅仅是编辑控制器。
-
编辑 config/routes.rb 并包含
match "/appointments/thank_you" => "appointments#thank_you"
-
您可能希望在控制器中插入
render
命令,这会将您带到必须创建的感谢视图...