[英]Rails period inside of url
我无法摆脱网址中的句号。 我已经找到了解决此问题的其他解决方案,但其中任何一个都是针对索引操作的。
这是一个网址的样子
/shared_songs.32 #current url structure
/shared_songs/32 #would like this format
这是我的routes.rb里面的内容
get 'shared_songs/:note_id' => "shared_songs#show" #works fine
get 'shared_songs', to: "shared_songs#index", as: "shared_songs" #/shared_songs.32
我目前在index.html.erb文件中
link_to song.name, shared_songs_path(song)
任何想法如何解决这个问题?
发生这种情况的原因是因为您使用的URL帮助器在路径中没有任何动态段(:id,:user_id等),但是无论如何都为它提供了一个值(歌曲)。 Rails不知道要做什么,它使用该值作为格式,这就是为什么您最终使用/ shared_songs / 32的原因
shared_song_path(song)
不起作用,因为您当前没有名为shared_song
的路由。 正如一些评论所说,到目前为止,最简单的方法是
resources :shared_songs
这将为您提供一个功能正常的shared_songs_path
(对于索引,不需要任何arguments_和shared_song_path
(需要一个参数)。您将必须稍作更改,因为歌曲的ID将在params[:id]
而不是params[:note_id]
代替:
link_to song.name, shared_songs_path(song)
做:
link_to song.name, shared_song_path(song)
song
,不是songs
如果您以RESTful方式定义路由,则可能会有所帮助:类似于resources :shared_songs
。 正如在Rails文档中更清楚地解释的那样 ,使用resources
助手将自动为相应的控制器动作设置适当的路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.