The whole code is from this question: Nested form validations not working in rails app
The validation works, but now the problem is with error_message. It is simply not showing up.
I found out that it won’t work because of redirect_to in my controller:
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
And I have to use render instead. But the question is how?
This message form is inside another models show page and if the message doesn’t pass validations, I want to see the error message in the same show page.
Can You help ?
Thanks!
Update:
rake routes
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"}
Then you should have a partial called
_edit.html.erbwithinviews/messagesthat contains your form and your code to render the errors.And your
edit.html.erbcan just be something likerender "edit"Quick comment:
you should change this:
to
And set
@message(@message = @message.new) in thenewaction. That way, you can use the same form for the new and edit action.You could also think about using a gem like
simple_formthat makes creating forms a lot easier, and you would have errors displayed inline.