hodale
hodale

Reputation: 747

Custom route yields dot instead of '?id='

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)

Upvotes: 1

Views: 370

Answers (3)

hodale
hodale

Reputation: 747

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!

Upvotes: 0

Richard Peck
Richard Peck

Reputation: 76774

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

Upvotes: 2

Brad Werth
Brad Werth

Reputation: 17647

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)

Upvotes: 2

Related Questions