【发布时间】: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