rails3 before_filter原因过滤链暂停为:验证呈现或重定向错误

时间:2013-04-27 09:09:22

标签: ruby-on-rails-3 filter before-filter

在我的index.html中,我使用<%= render'rate率/ rate_form'%> 呈现费率表格,表格是ajax。_rate_form的内容是:

   `<%= form_for [@image, Rate.new], :remote=>true do |f| %>  
<input type="hidden" name="image_id" value="<%= @image.id%>"/>
<button type="submit"> Rate</button>
<% end %>`

在我的Rate控制器中,我定义了一个before_filter,只有使用中签名才能创建速率。     class RatesController&lt; ApplicationController的      before_filter:authenticate,:only =&gt; :创建

def create
@rate=Rate.new(:rate=>1)

respond_to do |format|
  if @rate.save
    format.html { redirect_to(@rate) }
    format.js 
  end
end
end
在我的sesson_helper文件中:

 def authenticate
   redirect_to signin_url unless signed_in? 
 end

日志是:

Redirected to http://localhost:3000/signin
Filter chain halted as :authenticate rendered or redirected
Completed 302 Found in 3ms (ActiveRecord: 0.7ms)


Started GET "/signin" for 127.0.0.1 at 2013-04-28 12:36:56 +0300
Processing by SessionsController#new as JS
Rendered sessions/new.html.erb within layouts/application (1.3ms)
User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1
Rendered sessions/new.html.erb (1.2ms)
Rendered layouts/_nav.html.erb (4.8ms)
Rendered layouts/_search.html.erb (0.6ms)
Rendered layouts/_notice_div.html.erb (0.1ms)
Completed 200 OK in 26ms (Views: 24.3ms | ActiveRecord: 0.9ms)

当我点击Rate按钮时,控制台出错:Filter chain暂停为:deny_access呈现或重定向 你知道怎么解决吗?非常感谢!

1 个答案:

答案 0 :(得分:1)

所以你有一个在每次执行create动作之前运行的过滤器。它的作用是无条件地重定向。这实际上意味着没有人能够调用创建操作。我想在deny_access中应该进行一些检查以验证登录的会话,或者等等。

您在日志中看到的消息只会通知您过滤器已重定向。