[可能是一个简单的问题,但是我正在将Rails 6与webpacker一起使用,并且遇到这个问题,其中我的根URL以/#/而不是/出现。如果我尝试转到localhost:3000,它将自动重定向到localhost:3000 /#/,而我所有其他路由都将通过。我无法找到与该项目中的典型Rails应用程序不同的任何操作,而这会导致路由像这样工作。
我想将其放到干净的localhost:3000 /中,所以看起来更好,并且我相信/#/会将带有片段href的标签弄乱了,使它们重定向到索引页。
编辑:添加rails routes
api_session GET /api/session(.:format) api/sessions#show {:format=>:json}
DELETE /api/session(.:format) api/sessions#destroy {:format=>:json}
POST /api/session(.:format) api/sessions#create {:format=>:json}
api_users GET /api/users(.:format) api/users#index {:format=>:json}
POST /api/users(.:format) api/users#create {:format=>:json}
api_user GET /api/users/:id(.:format) api/users#show {:format=>:json}
PATCH /api/users/:id(.:format) api/users#update {:format=>:json}
PUT /api/users/:id(.:format) api/users#update {:format=>:json}
api_campaigns GET /api/campaigns(.:format) api/campaigns#index {:format=>:json}
POST /api/campaigns(.:format) api/campaigns#create {:format=>:json}
api_campaign GET /api/campaigns/:id(.:format) api/campaigns#show {:format=>:json}
PATCH /api/campaigns/:id(.:format) api/campaigns#update {:format=>:json}
PUT /api/campaigns/:id(.:format) api/campaigns#update {:format=>:json}
DELETE /api/campaigns/:id(.:format) api/campaigns#destroy {:format=>:json}
api_campaign_subs GET /api/campaign_subs(.:format) api/campaign_subs#index {:format=>:json}
POST /api/campaign_subs(.:format) api/campaign_subs#create {:format=>:json}
api_campaign_sub DELETE /api/campaign_subs/:id(.:format) api/campaign_subs#destroy {:format=>:json}
api_characters GET /api/characters(.:format) api/characters#index {:format=>:json}
POST /api/characters(.:format) api/characters#create {:format=>:json}
api_character GET /api/characters/:id(.:format) api/characters#show {:format=>:json}
PATCH /api/characters/:id(.:format) api/characters#update {:format=>:json}
PUT /api/characters/:id(.:format) api/characters#update {:format=>:json}
DELETE /api/characters/:id(.:format) api/characters#destroy {:format=>:json}
api_invites POST /api/invites(.:format) api/invites#create {:format=>:json}
api_invite PATCH /api/invites/:id(.:format) api/invites#update {:format=>:json}
PUT /api/invites/:id(.:format) api/invites#update {:format=>:json}
DELETE /api/invites/:id(.:format) api/invites#destroy {:format=>:json}
root GET / static_pages#root
development.log也没有特别说明。
Started GET "/" for ::1 at 2020-04-19 16:00:18 -0400
Processing by StaticPagesController#root as HTML
然后进行一些数据库调用
Rendered api/users/_user.json.jbuilder (Duration: 140.1ms | Allocations: 20249)
Rendered static_pages/root.html.erb within layouts/application (Duration: 150.1ms | Allocations: 21250)
Completed 200 OK in 217ms (Views: 204.9ms | ActiveRecord: 11.7ms | Allocations: 28894)
我最终确实以某种方式解决了这个问题。问题是我使用的是HashRouter,它使用URL中的哈希内容进行前端路由,但从未将该信息发送到服务器。
我通过切换到BrowserRouter并实现了全部路由以将所有内容重新路由到/ *路径来解决它。
但是,我知道过去我使用HashRouter制作了单页应用程序,但没有将#添加到url中,并且可以与后端完美配合。我不确定这次我所做的与众不同,或者可能是由于Rails或React的更高版本。