整个代码来自这个问题:Nested form validations not working in rails app
验证有效,但现在问题出在 error_message 上。它根本没有出现。
我发现由于我的控制器中的 redirect_to 它不起作用:
class MessagesController < ApplicationController
def create
@trip = Trip.find(params[:trip_id])
@message = @trip.messages.create(params[:message])
if @trip.messages.create
MessageMailer.send_message(@message).deliver
redirect_to thank_you_path
else
redirect_to trip_path(@trip)
end
end
end
我必须改用渲染。但问题是如何?
此消息表单位于另一个模型显示页面中,如果消息未通过验证,我想在同一显示页面中查看错误消息。
你能帮我吗 ?谢谢!
更新:
耙路线
messages GET /messages(.:format) {:action=>"
index", :controller=>"messages"}
POST /messages(.:format) {:action=>"
create", :controller=>"messages"}
new_message GET /messages/new(.:format) {:action=>"
new", :controller=>"messages"}
edit_message GET /messages/:id/edit(.:format) {:action=>"
edit", :controller=>"messages"}
message GET /messages/:id(.:format) {:action=>"
show", :controller=>"messages"}
PUT /messages/:id(.:format) {:action=>"
update", :controller=>"messages"}
DELETE /messages/:id(.:format) {:action=>"
destroy", :controller=>"messages"}
trip_messages GET /trips/:trip_id/messages(.:format) {:action=>"
index", :controller=>"messages"}
POST /trips/:trip_id/messages(.:format) {:action=>"
create", :controller=>"messages"}
new_trip_message GET /trips/:trip_id/messages/new(.:format) {:action=>"
new", :controller=>"messages"}
edit_trip_message GET /trips/:trip_id/messages/:id/edit(.:format) {:action=>"
edit", :controller=>"messages"}
trip_message GET /trips/:trip_id/messages/:id(.:format) {:action=>"
show", :controller=>"messages"}
PUT /trips/:trip_id/messages/:id(.:format) {:action=>"
update", :controller=>"messages"}
DELETE /trips/:trip_id/messages/:id(.:format) {:action=>"
destroy", :controller=>"messages"}