[英]Rails period inside of url
I am having trouble getting rid of a period inside of my url. 我无法摆脱网址中的句号。 I've look up others solution to this problem but either of them were for the index action.
我已经找到了解决此问题的其他解决方案,但其中任何一个都是针对索引操作的。
Here is what a url looks like 这是一个网址的样子
/shared_songs.32 #current url structure
/shared_songs/32 #would like this format
Here is what is inside of my routes.rb 这是我的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
Inside of my index.html.erb file I currently have 我目前在index.html.erb文件中
link_to song.name, shared_songs_path(song)
Any idea how to resolve this problem? 任何想法如何解决这个问题?
The reason this is happening is because you are taking a url helper that doesn't have any dynamic segments (:id, :user_id etc.) in the path, but you're giving it a value anyway (song). 发生这种情况的原因是因为您使用的URL帮助器在路径中没有任何动态段(:id,:user_id等),但是无论如何都为它提供了一个值(歌曲)。 Not knowing what else to do with it, rails uses that value as the format, which is why you end up with /shared_songs/32
Rails不知道要做什么,它使用该值作为格式,这就是为什么您最终使用/ shared_songs / 32的原因
shared_song_path(song)
doesn't work because you don't current have a route called shared_song
. shared_song_path(song)
不起作用,因为您当前没有名为shared_song
的路由。 As several of the comments say, by far the easiest way is to do 正如一些评论所说,到目前为止,最简单的方法是
resources :shared_songs
This will give you a functioning shared_songs_path
(for the index, doesn't expect any arguments_ and shared_song_path
(requires a parameter). You'll have to change your controller slightly because the the id of the song will be in params[:id]
instead of params[:note_id]
这将为您提供一个功能正常的
shared_songs_path
(对于索引,不需要任何arguments_和shared_song_path
(需要一个参数)。您将必须稍作更改,因为歌曲的ID将在params[:id]
而不是params[:note_id]
Instead of: 代替:
link_to song.name, shared_songs_path(song)
Do: 做:
link_to song.name, shared_song_path(song)
song
, not songs
song
,不是songs
It might help if you define your routes in a RESTful manner: something like resources :shared_songs
. 如果您以RESTful方式定义路由,则可能会有所帮助:类似于
resources :shared_songs
。 As explained much more clearly in the Rails docs , using the resources
helper will automatically set up appropriate routes to the corresponding controller actions. 正如在Rails文档中更清楚地解释的那样 ,使用
resources
助手将自动为相应的控制器动作设置适当的路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.