How to return failed validation parameters to a rails model form that doesn't know what page it's on? -
i have small contact request form appears on every page in site. form submits requestscontroller create method. if goes well, request saved in requests table , e-mail sent. if validation fails, redirect referrer page.
here create action...
def create @request = request.new(params[:request]) @location = location.find(params[:location_id]) if @request.save podsmailer.contact_request(@request, @location).deliver podsmailer.contact_confirmation(@request, @location).deliver flash[:notice] = "thanks! message sent. contact shortly." redirect_to :back else flash[:notice] = "errors -- please check form" flash[:errors] = @request.errors redirect_to ?????? end end
my question when validation fails, how redirect referrer , send values entered form user? can't use named route because don't know request coming from.
"redirect_to :back" works fine itself. can't figure out how send parameters. flash hash makes through fine. there's got simple i'm missing.
i have tried passing params hash (:params => params[:request]) every combination can imagine of redirect_to :back, url_for, request.referer... no luck.
i imagine there way manually build url , tack on query parameters, though i'm not sure how that. wondering if there rails trick passing parameters in situation. also, i'd implement server-side solution before adding ajax solution.
any appreciated. first question here. i've done correctly.
thanks--
best here use ajax
, validation on client (that don't mean deny server vilidation).
but suggest hacky solution:
def create @request = request.new(params[:request]) @location = location.find(params[:location_id]) if @request.save podsmailer.contact_request(@request, @location).deliver podsmailer.contact_confirmation(@request, @location).deliver flash[:notice] = "thanks! message sent. contact shortly." redirect_to :back else flash[:notice] = "errors -- please check form" flash[:errors] = @request.errors referer = request.referer.gsub(/\?.*/, "") redirect_to referer + "?" + params[:request].map{|k,v| "request[#{k}]=#{v}"}.join("&") end end
but should in request form this:
<%= form_for request.new(params[:request]) |f| %> ...
or remove controller
Comments
Post a Comment