简体   繁体   English

在Rails中重新呈现动作后,如何在页面URL中保持相同的控制器和动作?

[英]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控制器中, createupdate操作中的验证错误将仅呈现原始模板而无需重定向。 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.

相关问题 Ruby on Rails - 使用原始参数在失败的 controller 操作上重新渲染表单 - Ruby on Rails - Re-Rendering a form on a failed controller action with the original parameters 渲染操作:在控制器的更新方法中编辑不重新渲染表单 - Render action: edit not re-rendering form in update method of controller 调用自定义操作,而无需使用Rails link_to重新渲染页面 - Calling custom action without re-rendering page with Rails link_to 如何创建Rails控制器动作? - How do I create a rails controller action? Rails在不同控制器中的渲染动作 - Rails Rendering Action in Different Controller Rails控制器动作+渲染错误 - rails controller action + rendering bug 如何使用URL中的令牌限制对控制器的一个操作的访问 - Rails 3? - How do I restrict access to one action of a controller using a token in the URL - Rails 3? Rails 4-如何基于当前路径显示同一控制器动作的不同视图? - Rails 4 - How do I show a different view for the same controller action, based on the current route? 如何使用页面ID /名称加载正确的控制器操作? (Ruby on Rails 4) - How do I use a page id/name to load the correct controller action? (Ruby on Rails 4) 如何调用动作,从js传递值到该动作并在Rails 3的页面上重新呈现js? - How can I call an action, pass a value to the action from js and re-render the js on a page in rails 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM