gpt4 book ai didi

ruby-on-rails - before_filter 在使用 devise 和 jquerymobile 时不重定向

转载 作者:行者123 更新时间:2023-12-02 02:19:33 24 4
gpt4 key购买 nike

当使用名为 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

最佳答案

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

data-ajax="false"

在 link_to 选项中,即:

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

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

关于ruby-on-rails - before_filter 在使用 devise 和 jquerymobile 时不重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8956234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com