没有redirect_to页面出错

时间:2014-01-10 15:48:10

标签: javascript jquery ruby-on-rails redirect

成功时,表单远程提交并正确加载index.js.erb(生活很好)

然而,在错误(其他)上没有任何反应。我想做什么,在错误加载不同的文件或向index.js.erb添加逻辑时。但是,它只是失败而且无法加载。

错误 无法加载资源:服务器响应状态为500(内部服务器错误)我知道在执行救援后收到406错误

我理解这是因为它找不到事件,但我如何确保它加载我的index.js.erb-所以我可以反过来显示一个通知用户的模态

事件控制器

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 %>

服务器日志

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)

2 个答案:

答案 0 :(得分:0)

尝试开始救援块

  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

答案 1 :(得分:0)

如果您的视图中有来自@event的电话,那么您需要处理它们。如果在页面上你有(比方说)提交值的表单,那么只需确保视图提供@event = Event.new就足够了。但是,可能情况并非如此,因此如果@event未定义,您希望在视图阶段通过提供在页面上下文中有意义的替代值来进行抢救。类似的东西:

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

而不仅仅是

<%= @event %>

编辑:新控制器代码

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

我并不认为这是一种很好的做事方式。表单应该是分开的,这样你就可以获得可以查询的单独的params,指定if(say)params.has_key? :foo然后执行某些操作,反之亦然。

<%= 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 %> 

这样你可以说如果params.has_key? :event_code为假,那么找params.has_key? :speaker_code,然后失败,执行else分支。