简体   繁体   中英

Doesn't redirect_to page on error

On success, the form submits remotely and correctly loads index.js.erb (Life is well)

However on error (the else) nothing happens. What I want to do, on error load a different file or add logic to index.js.erb. However, it just fails and doesn't load.

Error Failed to load resource: the server responded with a status of 500 (Internal Server Error) I know receive a 406 Error, after implementing the rescue

( I understand that is because it doesn't find an event, but how do I ensure it loads my index.js.erb- so I can in turn display a modal notifying the user )

Events Controller

def index
  respond_to do |format|
    if @event = Event.find_by_event_code(params[:checkin])
      format.html { redirect_to @event, success: "You have found your event" }
      format.js
    elsif @event = Event.find_by_speaker_code(params[:checkin])
      format.html { redirect_to @event, success: "Welcome Super User. You have found your event" }
      format.js
    else
      format.html { redirect_to @event, error: "No event by that event code" }
      format.js
    end
  end
end

index.js.erb

$("#checkin-popup").remove();
$("#checkin-confirm-popup").html("<%= escape_javascript(render("/events/index.html.erb")) %>")
$('#checkin-confirm-popup').modal('show');

button.html

<%= simple_form_for @event, remote: true ,:method => 'get' do |e| %>
    <div class="talknumber"><%= text_field_tag :checkin, params[:search] %></div>
  <%= e.button :submit, "Checkin", :class => "btn btn-default" %>
<% end %>

server log

Started GET "/events?utf8=%E2%9C%93&checkin=1212122&commit=Checkin" for 127.0.0.1 at 2014-01-10 11:43:39 -0500
Processing by EventsController#index as JS
  Parameters: {"utf8"=>"✓", "checkin"=>"1212122", "commit"=>"Checkin"}
   (0.5ms)  SELECT "users"."name" FROM "users"
  Event Load (0.5ms)  SELECT "events".* FROM "events" WHERE "events"."event_code" = 1212122 ORDER BY events.created_at ASC LIMIT 1
  Event Load (0.4ms)  SELECT "events".* FROM "events" WHERE "events"."speaker_code" = 1212122 ORDER BY events.created_at ASC LIMIT 1
Completed 406 Not Acceptable in 7ms (ActiveRecord: 1.4ms)

Try a begin rescue block

  def index
    respond_to do |format|
      begin
        if @event = Event.find_by_event_code(params[:checkin])
          format.html { redirect_to @event, success: "You have found your event" }
          format.js
        elsif @event = Event.find_by_speaker_code(params[:checkin])
          format.html { redirect_to @event, success: "Welcome Super User. You have found your event" }
          format.js
        end
      rescue
        format.html { redirect_to @event, error: "No event by that event code" }
        format.js
      end
    end
  end

If you have calls in your view that reference the @event then you need to deal with them. If on the page you've got (say) a form that submits values, then simply making sure that the view is supplied with @event = Event.new should suffice. However, probably that's not the case, so you want to rescue at the view stage by providing an alternative value that makes sense within the context of the page if @event is undefined. Something like:

<%= @event ? @event : 'No event' %>

rather than just

<%= @event %>

EDIT: new controller code

def index
 respond_to do |format|
    if @event = Event.find_by_event_code(params[:checkin])
      format.html { redirect_to @event, success: "You have found your event" }
      format.js
    else 
      if @event = Event.find_by_speaker_code(params[:checkin])
        format.html { redirect_to @event, success: "Welcome Super User. You have found your event" }
        format.js
      else
        format.html { redirect_to @event, error: "No event by that event code" }
        format.js
      end
    end
  end
end

I don't really think this is a great way of doing things though. The form should be separated so you get separate params you can query on, specifying that if (say) params.has_key? :foo params.has_key? :foo then do something, and vice versa.

<%= simple_form_for @event, remote: true ,:method => 'get' do |e| %>
  <div class="talknumber">
    <%= text_field_tag :event_code, params[:search] %>
    <%= text_field_tag :speaker_code, 'value here - I don't know what params[:search is] %>
  </div>
  <%= e.button :submit, "Checkin", :class => "btn btn-default" %>
<% end %> 

This way you can say that if params.has_key? :event_code params.has_key? :event_code is false, then look for params.has_key? :speaker_code params.has_key? :speaker_code , and failing that, execute the else branch.

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