[英]How do I maintain the same controller & action in a page URL upon re-rendering an action in Rails?
I am using AuthLogic to authenticate users in my rails app. 我正在使用AuthLogic在Rails应用程序中对用户进行身份验证。 That part is set up and workign properly. 该部分已设置并正常工作。
I have the following route defined: 我定义了以下路线:
map.login '/account/login', :controller => :user_sessions, :action => :new
Calling rake routes returns what I expect: 调用耙路径返回我期望的结果:
login /account/login {:controller=>"user_sessions", :action=>"new"}
When someone submits a login, it calls UserSessionsController.create: 当有人提交登录名时,它将调用UserSessionsController.create:
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default account_url
else
render :action => :new
end
end
If @user_session.save fails, the appropriate error messages appear on the screen. 如果@ user_session.save失败,则屏幕上会显示相应的错误消息。 However, the browser URL also changes to "http://localhost:3000/user_session" instead of staying on "http://localhost:3000/account/login". 但是,浏览器URL也会更改为“ http:// localhost:3000 / user_session”,而不是停留在“ http:// localhost:3000 / account / login”上。
I assume the problem is what I am feeding to the render method. 我认为问题是我正在提供给render方法。 What should I be feeding it? 我应该喂什么呢?
This is actually the intended behavior for this process. 这实际上是此过程的预期行为。 In a standard scaffolded RESTful controller, a validation error in the create
and update
actions will simply render the original template without redirecting. 在标准的脚手架RESTful控制器中, create
和update
操作中的验证错误将仅呈现原始模板而无需重定向。 This results in what you are seeing – the new
template will be displayed with the create
action's URL in the URL bar. 这样便得到了您所看到的内容– new
模板将在URL栏中显示带有create
action的URL。 The reason for this is that in order to display information to the user about what errors occurred, the view must have access to the invalid model object, which is @user_session
in your case. 这样做的原因是,为了向用户显示有关发生了什么错误的信息,视图必须有权访问无效的模型对象(在您的情况下为@user_session
。
You can use redirect_to
instead of render
if you want to force a redirect to the original URL, but this will cause you to lose information about the errors. 如果要强制重定向到原始URL,可以使用redirect_to
而不是render
,但这将导致您丢失有关错误的信息。 You would need to manually persist the errors in the session, which would be messy. 您将需要在会话中手动保留错误,这很麻烦。 My advice is not to worry about the fact that the URL doesn't match that of the original as this is pretty standard in all Rails apps. 我的建议是不要担心URL与原始URL不匹配的事实,因为这在所有Rails应用程序中都是非常标准的。
Just adding solution for Rails 4 (based on Shaun's answer here): 只需添加Rails 4的解决方案(基于Shaun在这里的答案):
Add new route to routes file: 将新路由添加到路由文件:
post '/carts/new' => 'carts#create', as: :create_post 发表'/ carts / new'=>'carts#create',如::create_post
Add url: create_post_path to form tag 将网址:create_post_path添加到表单标签
Done. 完成。
After further digging, I found the solution in another StackOverflow question: Use custom route upon model validation failure 进一步挖掘之后,我在另一个StackOverflow问题中找到了解决方案: 在模型验证失败时使用自定义路由
I simply modified my routes to add a new one for posing to '/account/login': 我只是修改了路线,以添加一个新的路线来构成“ / account / login”:
map.login '/account/login', :controller => :user_sessions, :action => :new, :conditions => {:method => :get}
map.login_post '/account/login', :controller => :user_sessions, :action => :create, :conditions => {:method => :post}
Then, I updated my view to utilize the new route: 然后,我更新了视图以利用新路线:
<% form_for @user_session, :url => login_post_path do |f| %>
This works perfectly. 这很完美。 A failed login gives the appropriate error messages and maintains the '/account/login' URL. 登录失败会给出相应的错误消息,并维护“ / account / login” URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.