Skip to content

Instantly share code, notes, and snippets.

@redrory
Last active January 2, 2016 15:09
Show Gist options
  • Save redrory/8321224 to your computer and use it in GitHub Desktop.
Save redrory/8321224 to your computer and use it in GitHub Desktop.
Currently, when you find an event (if - successful ). It loads my modal via index.js.erb That works fine, however I want to load a different set of modals if an event is not found (else). However, on the else condition; it doesn't load the index.js.erb
<h3>Enter your talk code</h3>
<%= 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 %>
def index
if @event = Event.find_by_event_code(params[:checkin])
flash[:success] = "You have found your event"
elsif @event = Event.find_by_speaker_code(params[:checkin])
flash[:success] = "Welcome Co-Speaker. You have found your event"
else
flash[:error] = "No event by that event code"
end
respond_to do |format|
format.html { redirect_to @event }
format.js
end
end
$("#checkin-popup").remove();
$("#checkin-confirm-popup").html("<%= escape_javascript(render("/events/index.html.erb")) %>")
$('#checkin-confirm-popup').modal('show');
@TheNaoX
Copy link

TheNaoX commented Jan 9, 2014

You can force the rendering by moving the if clause inside the respond_to block, look:

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 Co-Speaker. 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

@TheNaoX
Copy link

TheNaoX commented Jan 9, 2014

And a little bit of refactor:

 def index
   @event = Event.find_by_event_code(params[:checkin]) || Event.find_by_speaker_code(params[:checkin])
   respond_to do |format|
     if @event.event_code == params[:checkin]
       format.html { redirect_to @event, success: "You have found your event"  }
       format.js
     elsif @event.speaker_code == params[:checkin]
       format.html { redirect_to @event, success: "Welcome Co-Speaker. 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

@redrory
Copy link
Author

redrory commented Jan 9, 2014

See revision 2

@TheNaoX
Copy link

TheNaoX commented Jan 9, 2014

You're missing here the e https://gist.github.com/redrory/8321224#file-button-html-erb-L3

    <div class="talknumber"><%= e.input :checkin, input_html: { value: params[:search] }%></div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment