[英]Rails: redirecting to wrong path
@trip = Trip.new(trip_par)
if @trip.save
redirect_to trips_index_path
flash[:notice] = "success"
else
render trips_new_path
flash[:notice] = "ERROR"
end
当我从行程/新建发送“表格”时,我得到了“成功”闪存,并将其发送到“行程/索引”路径,但从“行程/显示”获取了视图。 我尝试了几种组合,但仍然相同。 我的路线:
get 'trips/show', to: 'trips#show'
get 'trips/new'
get 'trips/edit'
get 'trips/delete'
get 'trips/index', to: 'trips#index'
可能是路由/动作控制器有问题还是我的错误?
几句话:
get 'trips/delete'
不应为get而是delete 。 为了使操作更简单,我建议您用一个单独的行替换当前routes.rb
的5行: resources :trips
所有路由技巧都可以在这里找到
if @trip.save flash[:notice] = "success" redirect_to trips_index_path else flash[:notice] = "ERROR" render trips_new_path end
:notice
。 根据您使用的框架或约定,成功应该是:success
,错误应该是:alert
或:danger
。 尝试像这样更改路线:
get 'trips' => 'trips#index'
或者只是使用“资源”会更容易,它会自动为您提供索引,显示等的所有路线...
resources: :trips
尝试在浏览器中的URL上附加rails / info / routes来检查类似这样的内容
http://localhost:3000/rails/info/routes
在列表中查找您的路线,或使其更容易找到,您可以像这样创建自定义路线
get 'trips' => 'trips#index', as: :custom_trips_index
问题是“行程/索引”与“行程/:id”匹配,因此自动将我跟踪到“行程/显示”,因为行程/ id ==行程/显示。 我手动添加了它,所以我不得不删除这些不必要的路由,没关系。 感谢您的提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.