Render the current page and show notice when error occurs?
I have an ActiveModel contact form in my "show" view.
My simple_form_for for however doesn't validate the fields. I saw in
another question that if we redirect, we lose the validation.
How can I stay in the same page and still show the flash notice message
when I do not fill the required fields? Something like
redirect_to :back
but with the notice.
def show
@post = Post.find(params[:id])
@message = Message.new
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
def newmessage
@message = Message.new
end
def contact
@message = Message.new(params[:message])
@string = params[:receiver]
if @message.valid?
PostMailer.contact_poster(@message, @string).deliver
redirect_to(root_path, :notice => "Sent.")
else
flash.now[:notice] = "Error. Please fill all fields"
redirect_to(?????, :notice => "Error.")
end
end
No comments:
Post a Comment