在使用devise和jquerymobile时,before_filter没有重定向

时间:2012-01-21 20:04:58

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 jquery-mobile devise

当使用名为format.mobile的格式/ mimetype时,我想执行一项操作,要求用户首先使用before_filter :authenticate_user使用登录,但它不会将我重定向到设计的登录页面。要做insted,它会在我的服务器日志中返回一条消息Completed 401 Unauthorized in 336ms并且不会将我重定向到登录页面。我正在使用jquerymobile为我的移动支持,如何进行重定向调用。

歌曲显示模板

<% reply = @song.replies.first(:select => 'id, yes', :conditions => ['user_id = ?', current_user]) %>

<div class="ui-body ui-body-a double-margin-top">
  <p class="small-fonts">
    Show your love or hate for this song.
  </p>
<fieldset class="ui-grid-a">
                    <%= form_for(:reply, :remote => true, :url => song_replies_path(song)) do |f| %>
                        <%= f.hidden_field :yes, :value => Reply::Response::No %>
                        <div class="ui-block-a">
                          <%= submit_tag "Hate", "data-role"=>"button", "data-icon"=>"trash", "data-theme"=>"b" %>
                        </div>
                    <% end %>
                    <%= form_for(:reply, :remote => true, :url => song_replies_path(song)) do |f| %>
                        <%= f.hidden_field :yes, :value => Reply::Response::Yes %>
                        <div class="ui-block-b">
                          <%= submit_tag "Love", "data-role"=>"button", "data-icon"=>"love", "data-theme"=>"b" %>
                        </div>
                    <% end %>
</fieldset>
</div>


class RepliesController < ApplicationController

  before_filter :authenticate_user!
  def create
    @reply = Reply.new(params[:reply])
    @reply.song_id = params[:song_id]
    @reply.user = current_user

    respond_to do |format|
      if @reply.save
        @song = Song.find(params[:song_id])
        format.html { redirect_to @song }
        format.js
        format.mobile {redirect_to @song}
      else
        format.html { redirect_to home_path }
      end
    end
  end

  # PUT /questions/replies/1
  def update
    @reply = Reply.find(params[:id], :include => :song)
    @song = @reply.song

    was_yes = @reply.yes?
    now_yes = params[:reply][:yes].to_i == Reply::Response::Yes

    respond_to do |format|
      if @reply.update_attributes(params[:reply])
        # Make sure we keep the question's response cache updated
        if was_yes and not now_yes
          @song.increment(:no_count)
          @song.decrement(:yes_count)
          @song.save
        elsif not was_yes and now_yes
          @song.increment(:yes_count)
          @song.decrement(:no_count)
          @song.save
        end

        format.html { redirect_to @song }
        format.js
        format.mobile { redirect_to @song }
      else
        format.html { redirect_to @song }
      end
    end
  end

end

服务器日志

Started POST "/songs/12/replies" for 127.0.0.1 at 2012-01-21 21:00:56 +0100
  Processing by RepliesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xT1pLXOCmqAf8wuflvesgeUeXuBBpNsfCXf+JMD64rM=", "reply"=>{"yes"=>"1"}, "commit"=>"Love", "song_id"=>"12"}
Completed 401 Unauthorized in 336ms

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,因为我使用的是jquery mobile,它使用了对link_to方法的AJAX调用。 就我而言,我通过在链接中添加以下内容使其成为常规link_to(非基于AJAX)

data-ajax="false"

在link_to选项中,即:

link_to 'Model', {:controller => "models", :action => "any_action"}, "data-ajax" => "false"

我希望有一天可以帮助某人。 干杯! Yohann。