【问题标题】:before_filter not redirecting when using devise and jquerymobile使用 devise 和 jquerymobile 时 before_filter 不重定向
【发布时间】:2012-01-21 20:04:57
【问题描述】:

当使用名为 format.mobile 的格式/mimetype 时,我想执行要求用户首先使用before_filter :authenticate_user 登录的操作,它不会将我重定向到设计应该执行的登录页面安装它会在我的服务器日志中返回一条消息,上面写着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

【问题讨论】:

  • 如果不发布一些示例代码、项目链接或jsfiddle.net,很难看到完整的问题
  • 刚刚更新了代码示例,但问题来自之前的过滤器,它没有将我重定向到登录页面。它应该正常进行
  • 我认为设计不会自动使用移动格式重定向。
  • 我遇到了同样的问题。我在做这个工作。你能在哪里解决它?

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


【解决方案1】:

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

data-ajax="false"

在link_to选项中,即:

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

我希望有一天能对某人有所帮助。 干杯! 约汉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 2013-04-02
    • 2016-10-17
    相关资源
    最近更新 更多