简体   繁体   中英

Rails nested form error_messages not displaying

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"} 
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.

render "/other_model/show"

渲染会保留当前的实例变量,因此,如果您的“ if”语句失败,验证将为@trip变量加载错误,并将其反映在新渲染的表单中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM