Reputation: 417
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"}
Upvotes: 0
Views: 901
Reputation: 21884
class MessagesController < ApplicationController
def create
@trip = Trip.find(params[:trip_id])
@message = @trip.messages.build(params[:message])
if @message.save
MessageMailer.send_message(@message).deliver
redirect_to thank_you_path
else
render "edit"
end
end
end
Then you should have a partial called _edit.html.erb
within views/messages
that contains your form and your code to render the errors.
And your edit.html.erb
can just be something like render "edit"
Quick comment:
you should change this:
form_for([@trip, @trip.messages.build])
to
form_for([@trip, @message])
And set @message
(@message = @message.new
) in the new
action. That way, you can use the same form for the new and edit action.
You could also think about using a gem like simple_form
that makes creating forms a lot easier, and you would have errors displayed inline.
Upvotes: 1
Reputation: 4566
render "/other_model/show"
Rendering preserves the current instance variables so if your "if" statement fails, the validations will load the @trip
variable with the errors and that will be reflected in the newly rendered form.
Upvotes: 0