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
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
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). 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
shared_song_path(song)
doesn't work because you don't current have a route called 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]
Instead of:
link_to song.name, shared_songs_path(song)
Do:
link_to song.name, shared_song_path(song)
song
, not songs
It might help if you define your routes in a RESTful manner: something like 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.