Ruby-on-rails 过滤器链因 [:login_required] 渲染_或重定向而停止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2714537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filter chain halted as [:login_required] rendered_or_redirected
提问by Magicked
Hopefully I can explain this well enough, but please let me know if more information is needed!
希望我能很好地解释这一点,但如果需要更多信息,请告诉我!
I'm building a form where a user can create an "incident". This incident has the following relationships:
我正在构建一个表单,用户可以在其中创建“事件”。本次事件有以下关系:
- belongs_to: customer (customer has_many incidents)
- belongs_to: user (user has_many incidents)
- has_one: incident_status (incident_status belongs to incident)
- 归属于:客户(客户有_许多事件)
- own_to: 用户(用户 has_many 事件)
- has_one: event_status(incident_status属于事件)
The form allows the user to assign the incident to a user (select form) and then select an incident status. The incident is nested in customer.
该表单允许用户将事件分配给用户(选择表单),然后选择事件状态。事件嵌套在客户中。
However, I'm getting the following in the server logs:
但是,我在服务器日志中得到以下信息:
Processing IncidentsController#create (for 127.0.0.1 at 2010-04-26 10:41:33) [POST]
Parameters: {"commit"=>"Create", "action"=>"create",
"authenticity_token"=>"YhW++vd/dnLoNV/DSl1DULcaWq/RwP7jvLOVx9jQblA=",
"customer_id"=>"4", "controller"=>"incidents", "incident"=>{"title"=>"Some Bad Incident",
"incident_status_id"=>"1", "user_id"=>"2", "other_name"=>"SS01-042310-001"}}
User Load (0.3ms) SELECT * FROM "users" WHERE ("users"."id" = 2) LIMIT 1
Redirected to http://localhost:3000/session/new
Filter chain halted as [:login_required] rendered_or_redirected.
Completed in 55ms (DB: 0) | 302 Found [http://localhost/customers/4/incidents]
It looks to me like it's trying to gather information about the user, even though it already has the id (which is all it needs to create the incident), and the user may not have permission to do a select statement like that? I'm rather confused.
在我看来,它正在尝试收集有关用户的信息,即使它已经拥有 id(这是创建事件所需的全部内容),并且用户可能无权执行这样的选择语句?我比较困惑。
Here is the relevant (I think) information in the Incident controller.
这是事件控制器中的相关(我认为)信息。
before_filter :login_required, :get_customer
def new
@incident = @customer.incidents.build
@users = @customer.users
@statuses = IncidentStatus.find(:all)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @incident }
end
end
def create
@incident = @customer.incidents.build(params[:incident])
respond_to do |format|
if @incident.save
flash[:notice] = 'Incident was successfully created.'
format.html { redirect_to(@incident) }
format.xml { render :xml => @incident, :status => :created, :location => @incident }
else
format.html { render :action => "new" }
format.xml { render :xml => @incident.errors, :status => :unprocessable_entity }
end
end
end
Just as an FYI, I am using the restful_authentication plugin.
仅供参考,我正在使用 restful_authentication 插件。
So in summary, when I submit the incident creation form, it does not save the incident because it halts. I'm still very new to rails, so my skill at diagnosing problems like this is still very bad. I'm going in circles. :)
所以总而言之,当我提交事件创建表单时,它没有保存事件,因为它停止了。我对 Rails 还是很陌生,所以我诊断此类问题的技能仍然很差。我在兜圈子。:)
Thanks in advance for any help. Please let me know if more information is needed and I'll edit it in!
在此先感谢您的帮助。如果需要更多信息,请告诉我,我会编辑它!
回答by Salil
Just use following in your controller.
只需在您的控制器中使用以下内容。
before_filter :login_required, :except=>[:new, :create]
回答by freemanoid
Or you can skip this filter:
或者您可以跳过此过滤器:
skip_before_filter :login_required, only: [:new, :create]

