如何在前端提交新记录后重定向到索引? [Ruby我的 2020.2.3,Ruby.2.7.2p137,gem 3.1.2]

How do I redirect the to index after submitting a new record at the front-end? [Rubymine 2020.2.3, Ruby.2.7.2p137, gem 3.1.2]

我正在为一个大学项目课程而苦苦挣扎,尽管同事和导师提供了建议,但我已经被这个错误困扰了几个星期。

在我的给定 table 的创建方法中。我试图让包含新记录条目表单的页面在保存后重定向回索引页面。相反,我被重定向到这个错误,突出显示 @courier=Courier.new(courier_new_path) 并指出它不是哈希的错误,并且不会将我重定向回索引。但是,当手动搜索索引时,我看到数据字符串确实会更新。

I have tried renaming the path label, but Rubymine prompt suggestions appear limited, and any further deviation would cause a different error

控制器页面(courier_controller.rb)中的创建方法如下:

def create
@courier=Courier.new(params.require(:courier).permit(:courier_name,:courier_email))
@courier.save
redirect_to courier_path(@courier)
@courier=Courier.new(courier_new_path)
if @courier.save
  redirect_to(:controller=>'courier' ,:action=>'index')
else
  render('new')
end 
end

这里是表单页面的代码(courier/new/html.erb):

<h1>Courier#new</h1>
<p>Find me in app/views/courier/new.html.erb</p>
<%= form_with scope: :courier, :url => {:action => 'create'}, local: true do |f| %>

  <p>
    <%= f.label :courier_name %><br/>
    <%= f.text_field :courier_name %>
  </p>

  <p>
    <%= f.label :courier_email %><br/>
    <%= f.text_field :courier_email %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

我尝试将@courier 重命名为 Courier.new(courier_create_path) 或 Courier.nww(courier_path),我尝试使用散列形式查找参数,但是none 似乎模棱两可,也没有 translatable 作为我的问题的解决方案。

任何建议都会有所帮助。这是大学项目的一部分,作为一名多媒体学生,与同龄人相比,在编程方面并不那么精明,我非常感谢可以尝试的建议。

非常感谢。

有没有人试图解释您的创建方法中实际发生了什么?我在每一行之后添加了注释以说明该行在做什么。

def create
  @courier=Courier.new(params.require(:courier).permit(:courier_name,:courier_email))
# use the params to build a new courier record (the permit part is usually in a separate method that other methods can access but it works this way)
  @courier.save
  # Save the new courier record to the database
  redirect_to courier_path(@courier)
  # Send the user back to the "show" page of the courier record (not index!)
  @courier=Courier.new(courier_new_path)
  # this makes no sense, you are trying to create a new courier object using a path method
  # Basically you are saying: @courier = Courier.new('/courier/new')
  if @courier.save
   #you are trying to save this record that will fail because you can't create a courier by passing it a url
    redirect_to(:controller=>'courier' ,:action=>'index')
     #send the user to the index page of the courier views. 
  else
    render('new')
     #something went wrong so go back to the new courier form.
  end 
end

当您的程序到达第 4 行时 redirect_to courier_path(@courier) 它将退出 create 方法并将用户发送到 http://my_app:3000/couriers/1,其中数字 1 将是数据库中的 ID您刚刚创建的记录。这将与您应用程序中 app/views/couriers/show.html.erb 中的文件相关。听起来您想进入 http://my_app:3000/couriers,向用户显示文件 app/views/couriers/index。html.erb 不确定您为什么要在那行之后做任何事情。

另外,您遇到的错误也不清楚。您需要查看您的网络服务器控制台,您 运行 "rails s" 显示浏览器和您的应用程序之间的所有通信的地方。找到以实际错误开头的堆栈跟踪,并将其添加到上面的问题中(不要将其粘贴到评论中,它会太长且无法阅读)。

我想你只需要:

def create
  @courier=Courier.new(params.require(:courier).permit(:courier_name,:courier_email))
  if @courier.save
    redirect_to @courier #if you want to redirect to their 'show" page
  else
    render('new')
     #something went wrong so go back to the new courier form.
  end 
end

新程序员可能会感到困惑的是,Rails 在幕后做了如此多的“魔术”,如果您还不知道幕后发生的基本概念,就会感到非常困惑。你说的是 redirect_to(:controller=>'courier' ,:action=>'index') ,它专门说“转到快递员的索引页面”,但你可以说 redirect_to @courier ,它会假设你想转到显示你的快递员记录的页面刚刚创建。如果你真的想去显示所有信使的table,你可以用redirect_to :couriers替换它。符号:couriers告诉它去信使控制器的索引方法。