Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails http post失败后浏览器URL的更改_Ruby On Rails_Url - Fatal编程技术网

Ruby on rails http post失败后浏览器URL的更改

Ruby on rails http post失败后浏览器URL的更改,ruby-on-rails,url,Ruby On Rails,Url,我有一个登录页面,在该页面上验证是否成功。这是页面new.html.erb: <%=form_with scope: :session, url: sessions_path, local: true, html: {class: "login-form"} do |f| %> <%= f.label :email, t("session.new.email") %> <%= f.email_field :email %> <%= f.la

我有一个登录页面,在该页面上验证是否成功。这是页面
new.html.erb

<%=form_with scope: :session, url: sessions_path, local: true, html: {class: "login-form"} do |f| %>
  <%= f.label :email, t("session.new.email") %>
  <%= f.email_field :email %>

  <%= f.label :password, t("session.new.password") %>
  <%= f.password_field :password %>

  <%= f.submit t('session.new.login'), class: "submit" %>
<% end %>
在我的
routes.rb
中,我仅为此目的:

resources :sessions
在执行
rails routes
时,我有以下声明的路由:

现在我的问题是登录失败。在我的控制器中,在本例中,我在flash消息中添加一条消息,然后重新呈现相同的页面
new.html.erb
。但在浏览器中,登录请求帖子已在url
/sessions
上发送。问题是我浏览器上的当前URL变成了
/sessions
,而不是停留在
/sessions/new
。这就好像POST请求更改了我浏览器中的URL一样。但这实际上只是一个AJAX请求,不是吗

我发现这个现象也有同样的奇怪之处(我不是作者)

我已经找到了一个解决方法,但我更愿意避免使用它,并理解bevahior。如果我将路线替换为以下内容,则此操作有效:

get    '/login',   to: 'sessions#new'
post   '/login',   to: 'sessions#create'
我可以理解为什么这样做:get和post url是相同的,因此浏览器不会更改其url

你知道吗

编辑:

我终于找到了解决办法。我不确定这是否是“rails方式”,但它的工作原理与预期一致。我刚刚更改了控制器,使其重定向到同一页面,并通过闪存请求传输登录失败信息:

def create
  # Find the user with the matching email
  user = User.find_by(email: params[:session][:email].downcase)

  # Check the user exists in DB and that the provided password matches
  if user && user.authenticate(params[:session][:password])
    # Log the user through the session helper
    log_in user
    # Redirect to the hive
    redirect_to ideas_path
  else
    # The authentication failed. Display an error message through a flash
    # message after redirect to the same page
    redirect_to new_session_path, alert: I18n.t('session.new.invalid_credentials')
  end
end

提交表单时,浏览器会对/sessions端点执行常规HTTP POST REQUEST。没有阿贾克斯

此POST请求的路由配置方式将由会话#创建操作处理

注意这里的代码。您将看到happy path(成功登录)调用。如果出现登录错误,控制器将调用

区别在于,在第一种情况下,响应是浏览器遵循的302重定向。这就是您在浏览器中看到URL更改的原因。在第二种情况下,响应只有200 OK,有一堆HTML供浏览器呈现。URL不会更改,因为浏览器未被指示在其他位置导航


如果您感兴趣,可以在浏览器中找到一个。

感谢您的回答和相关文档。我忽略了一点:为什么在我提交POST请求时浏览器的URL会改变?我认为这应该只适用于GET请求。此外,对于我的用例来说,RoR的好方法是什么?我一直在试图找到一些合适的文档来解释它,这些文档会比我更好,但是运气不好。浏览器访问
/sessions/new
。然后提交表单,这样浏览器知道的最后一个URL就是
/sessions
。这就是从服务器请求的“资源”。服务器会响应,因为它不会重定向,所以浏览器将使用该URL。在浏览器中打开开发工具并检查服务器响应。响应URL是
/sessions
,对吗?希望它有意义:/是的,你的回答帮助我理解了。现在,我只需要了解如何正确使用Rails,这样我就可以实现我的目标:)
def create
  # Find the user with the matching email
  user = User.find_by(email: params[:session][:email].downcase)

  # Check the user exists in DB and that the provided password matches
  if user && user.authenticate(params[:session][:password])
    # Log the user through the session helper
    log_in user
    # Redirect to the hive
    redirect_to ideas_path
  else
    # The authentication failed. Display an error message through a flash
    # message after redirect to the same page
    redirect_to new_session_path, alert: I18n.t('session.new.invalid_credentials')
  end
end