the respond_to block in create controller in rails app not redirecting on successful save... i'm sure simple solution, inexperienced rails , first time encountering problem.
the form set :remote => true, , controller follows...
def create @store = current_user.stores.new(store_params) respond_to |format| if @store.save format.html { redirect_to root_path } else format.html { flash[:alert] = "save failed! #{@store.errors.full_messages.join(";")}" render "new" } format.js {} end end end
and while i'm on subject, code else portion of conditional doesn't run either, except format.js {}
, run code in create.js.erb file (an alert, time being).
i'm working rails 4.2.5. can me understand why redirect , alert not working? thank you!
editing show solution based on rich's answer, here's solution came with:
controller:
def create @store = current_user.stores.new(store_params) flash.now[:alert] = "save failed! #{@store.errors.full_messages.join(";")}" unless @store.save respond_to |format| format.js end if @store.save flash[:notice] = "new store created" end end
create.js.erb
<% if flash.now[:alert] %> $("#alert_holder").empty(); $("#alert_holder").append("<%= j flash.now[:alert] %>"); <% else %> window.location.href = "<%= root_url %>"; <% end %>
note needed add quotes around redirect url.
on form success, page redirects root. on failure, error message flashes form not refreshed - answers user has entered remain.
remote: true
ajax request.
ajax javascript, , such invoke format.js
method:
def create @store = current_user.stores.new store_params respond_to |format| if @store.save format.js format.html { redirect_to root_path } else format.js format.html { flash[:alert] = "save failed! #{@store.errors.full_messages.join(";")}" render "new" } end end end
the format.js
method call /app/views/[:controller]/[:action].js.erb
file, fire of js have inside it.
if don't want have js
format handling response, you'll have away respond_to
, have you'd return (redirect_to
won't work).
ajax
there several stipulations need appreciate this:
- ajax cannot "redirect" (on own)
- ajax treated
js
in rails controller- you have "hack" flash working through
js
if don't have experience ajax, simple explanation it's "pseudo-request"; sends http request without having reload browser.
the pattern ajax simple: ajax request > server > ajax response
you cannot "redirect" via ajax unless parse response javascript. ajax acronym (asynchronous javascript , xml) suggests, response expected xml (ie no functionality).
--
to answer question, you'll need use flash.now
"flash" message, , handle response .js.erb
file:
def create @store = current_user.stores.new store_params flash.now[:alert] = "save failed! #{@store.errors.full_messages.join(";")}" unless @store.save respond_to |format| format.js format.html end end
this allow call...
#app/views/stores/create.js.erb <% if flash.now[:alert] %> alert("<%=j flash.now[:alert] %>"); <% end %> window.location.href = <%= root_url %>;
your new code can improved little :
def create @store = current_user.stores.new store_params if @store.save flash[:notice] = "new store created" else flash.now[:alert] = "save failed! #{@store.errors.full_messages.join(";")}" end respond_to |format| format.js end end
if wanted dry
code more, you'll want @ responders
gem:
#app/controllers/stores_controller.rb class storescontroller < applicationcontroller respond_to :js, only: :create def create @store = ... respond_with @store if @store.save end end
Comments
Post a Comment