我正在为一门大学项目课程而苦苦挣扎,尽管同事和导师提供了一些建议,但我已经被这个错误困扰了好几个星期。
在给定表的create方法中。我正在尝试将包含新记录条目的表单的页面在保存后重定向回索引页面。相反,我被重定向到这个错误,突出显示@courier=Courier.new(courier_new_path),并显示错误,说明它不是一个哈希,并且不会将我重定向回索引。但是,当手动搜索索引时,我发现数据字符串确实会更新。
下面是控制器页面(courier_controller.rb)中的create方法:
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),我尝试使用散列形式查找参数,但作为我的问题的解决方案,似乎没有一个是模棱两可的或可翻译的。
任何建议都会有所帮助。这是一个大学项目的一部分,作为一个多媒体学生,与其他同行相比,我在编程方面没有那么精明,我非常感谢可以尝试的建议。
在此之前,非常感谢您。
发布于 2020-12-17 16:05:27
有没有人试着解释一下你的create方法到底是怎么回事?我在每一行后面都添加了注释,以说明该行正在做什么。
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文件,我不确定您为什么要在该行之后执行任何操作。
也不清楚你得到的是什么错误。你需要查看你的app服务器控制台,在那里你运行"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
,它将假定您想要转到显示您刚刚创建的快递员记录的页面。如果您真的想转到显示所有信使的表,您可以将其替换为redirect_to :couriers
,符号:couriers
告诉它转到信使控制器的索引方法。
https://stackoverflow.com/questions/65342333
复制相似问题