简体   繁体   English

Rails 3路由保留自定义URL进行发布

[英]rails 3 routing keep custom url for post

i am fairly new to rails and want to keep the url the same for a user signing in if there is an error and the 'new' template is rendered 我是Rails的新手,如果出现错误并且呈现了“新”模板,则希望使用户登录时的网址保持不变

here are my routes 这是我的路线

resources :users, only: [:new, :create]
resources :sessions, only: [:new, :create, :destroy]

root to: 'pages#home'

match '/signin', to: 'sessions#new'
#match '/signin', to: 'sessions#create', via: :post, as: :post_session
match '/logout', to: 'sessions#destroy'

and here is the sessions controller code 这是会话控制器代码

def new
end

def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
        sign_in user
        redirect_to root_url
    else
        flash.now[:error] = 'Invalid email or password'
        render 'new'
    end
end

as you can see,i have a custom route commented out to catch the post so that the render 'new' call keeps the /signin url, but when i do this, the flash messaging of an error does not render in the page (it does without that route though). 如您所见,我注释掉了一条自定义路由以捕获该帖子,以便render'new'调用保留/ signin url,但是当我这样做时,错误的Flash消息传递不会在页面中呈现(它却没有该路线)。 i tried to use flash without the now method and still was not seeing my message show up. 我尝试不使用now方法使用Flash,但仍然没有看到我的消息显示。 any ideas? 有任何想法吗?

EDIT: 编辑:

i tried the suggestions below and was still seeing the issue. 我尝试了以下建议,但仍然看到问题。 after looking at the access logs, the application was routing to the first signin route because it was defined with match and not get. 查看访问日志后,该应用程序将路由到第一个登录路由,因为它是使用match而不是get定义的。 my updated and working routes file now looks like this 我的更新后的工作路线文件现在看起来像这样

resources :users, only: [:new, :create]
#resources :sessions, only: [:new, :create, :destroy]

root to: 'pages#home'

match '/signin', to: 'sessions#new', via: :get
match '/signin', to: 'sessions#create', via: :post, as: :post_session
match '/logout', to: 'sessions#destroy', via: :delete

Take out the now, that shouldn't be needed. 立即删除,这不是必需的。 Your routes are probably conflicting. 您的路线可能有冲突。 Based on how you have the match lines setup, you can probably just remove the resources :sessions altogether, and then uncomment the match line. 根据您设置匹配线的方式,您可以完全删除resources :sessions ,然后取消注释匹配线。 That should take care of what you need. 那应该照顾您的需要。

Also, make sure to go back to your other questions and accept some answers. 另外,请确保回到您的其他问题并接受一些答案。 A 0% acceptance rate isnt as likely to attract answers. 0%的接受率并不太可能吸引答案。

Edit 编辑

Based on your comments, it might just not know what to render at this time when you removed the resources call. 根据您的评论,当您删除资源调用时,可能只是不知道此时呈现什么内容。 Try changing to: 尝试更改为:

render "sessions/new"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM