Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

before_filter not redirecting when using devise and jquerymobile

When using a format/mimetype called format.mobile and i want to perform an action that requires a user to use sign in first by using the before_filter :authenticate_user it does not redirect me to the signin page that devise is suppose to do insted it returns a message in my server log saying Completed 401 Unauthorized in 336ms and does not redirect me to the signin page. I am using jquerymobile for my mobile support, how can do the redirect call.

the songs show template

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

the server log

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
like image 214
Uchenna Avatar asked Dec 06 '25 12:12

Uchenna


1 Answers

I had the same issue, due to the fact that I was using jquery mobile, which uses AJAX calls for the link_to methods. In my case, I made it a regular link_to (non-AJAX based) by adding the following to my link

data-ajax="false"

in the link_to options, i.e:

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

I hope that helps someone someday. Cheers! Yohann.

like image 155
Yohann T. Avatar answered Dec 09 '25 03:12

Yohann T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!