1

I am adding a custon new action for my rails app by adding the following to my routes.rb:

resources :adventures do
  member do
    match :upvote, via: [:post, :delete]
    match :downvote, via: [:post, :delete]
  end
  get 'seed', on: :new
end

(you can ignore the voting piece, just wanted to show you the whole block)

  upvote_adventure POST|DELETE /adventures/:id/upvote(.:format)          adventures#upvote
downvote_adventure POST|DELETE /adventures/:id/downvote(.:format)        adventures#downvote
seed_new_adventure GET         /adventures/new/seed(.:format)            adventures#seed
        adventures GET         /adventures(.:format)                     adventures#index
                   POST        /adventures(.:format)                     adventures#create
     new_adventure GET         /adventures/new(.:format)                 adventures#new
    edit_adventure GET         /adventures/:id/edit(.:format)            adventures#edit
         adventure GET         /adventures/:id(.:format)                 adventures#show
                   PATCH       /adventures/:id(.:format)                 adventures#update
                   PUT         /adventures/:id(.:format)                 adventures#update
                   DELETE      /adventures/:id(.:format)                 adventures#destroy

but this:

      seed_new_adventure_path(@adventure_collection.id)

generates this:

http://localhost:3000/adventures/new/seed.6

instead of this:

http://localhost:3000/adventures/new/seed?id=6

I read a lot of posts with people getting dots instead of slashes, but none with adding a an additional new action. Am I doing something wrong, or do I need to add something more?

EDIT: I did make a mistake and did not mean to plurailze the adventure path (Is how I had it originally). The real problem is that all I needed to do was pass the id as a parameter.

Here is the path I was looking for:

  redirect_to seed_new_adventure_path(:id => @adventure_collection.id)
hodale
  • 717
  • 1
  • 6
  • 13
  • what if you change it to `seed_new_adventures_path(@adventure_collection)` do you get what you expect? – Doon Aug 09 '14 at 03:47
  • actually the on new, doesn't take and :id, as shown in your rake routes so the id you are supplying is being treated as the format. What are you trying to do with seed? should it perhaps be on :member ? or on :collection?, are you trying to seed this new adventure from an existing collection? – Doon Aug 09 '14 at 03:56
  • Nice, I didn't know about the `on` method before – Richard Peck Aug 09 '14 at 09:08

3 Answers3

2

It's because you are using the wrong pluralization.

In your example, you are using: seed_new_adventures_path(@adventure_collection.id)

But the route is properly described as: seed_new_adventure_path(@adventure_collection.id)

And will probably work fine and be more readable as: seed_new_adventure_path(@adventure_collection)

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
2

Routes

Although Brad Werth is correct (your route pluralization is incorrect), the big problem you have is what you're trying to achieve.

You have specified the following link:

domain.com/adventure/new/seed

This is a get request with no other parameters present. I don't understand why you're passing an object to this route? This is why you're receiving the .6 problem (because Rails cannot build the routes), instead of getting /6

After thinking about what you're trying to do, and I believe you can fix it as follows:

#config/routes.rb
resources :adventures do
   ...
   get "seed(/:id)", on: :new #-> domain.com/adventures/new/seed/6
end
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
0

OK, so in order to get this:

http://localhost:3000/adventures/new/seed?id=7

I need to pass a parameter to the link like this:

  seed_new_adventure_path(:id => @adventure_collection.id)

I just forgot how to pass parameters!

hodale
  • 717
  • 1
  • 6
  • 13